develooper Front page | perl.beginners | Postings from May 2011

Re: gettting rid of whitespace characters

Thread Previous | Thread Next
From:
John W. Krahn
Date:
May 10, 2011 13:17
Subject:
Re: gettting rid of whitespace characters
Message ID:
4DC99D44.7010605@shaw.ca
Irene Zweimueller wrote:
> Dear list,

Hello,

> I´m relatively new to Perl, so please be patient.

Welcome to Perl and the beginners list.

> I tied to get rid of whitespace characters by the following programme:
>
> my $i="		       USEFUL               	";
>
>
> if ($i =~ /\s/)
> {
> $i =~ s/\s*//;
> }

You don't need to use the same regular expression twice.

$i =~ s/\s+//g;

/\s+/ will match one or more whitespace character and will only preform 
the substitution if a match is found and the /g option means to match 
the pattern through the whole string, not just the first occurrence of 
the pattern.

> print "xxxxxxxxxx $i xxxxxxxxxx\n";



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About