gettting rid of whitespace characters

gettting rid of whitespace characters

am 10.05.2011 19:25:42 von Irene Zweimueller

Dear list,

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

I tied to get rid of whitespace characters by the following programme:

my $i=3D" USEFUL ";


if ($i =3D~ /\s/)
{
$i =3D~ s/\s*//;
}

print "xxxxxxxxxx $i xxxxxxxxxx\n";

and it removes the whitespace characters in front of USEFUL, but not thos=
e
behind..
Any suggestions?

kind regards
Irene Zweimueller


--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: gettting rid of whitespace characters

am 10.05.2011 19:31:13 von Shawn H Corey

On 11-05-10 01:25 PM, Irene Zweimueller wrote:
> I tied to get rid of whitespace characters by the following programme:
>
> my $i=" USEFUL ";

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

The /g modifier means to repeatly look for the pattern.

See:
perldoc perlretut and search for /Global matching/
perldoc perlre and search for /Modifiers/


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: gettting rid of whitespace characters

am 10.05.2011 19:32:10 von Leo Susanto

http://en.wikipedia.org/wiki/Trim_%28programming%29

$string =3D~ s/^\s+//; # remove leading whitespace
$string =3D~ s/\s+$//; # remove trailing whitespace

or

$string =3D~ s/^\s+|\s+$//g ; # remove both leading and trailing whites=
pace


On Tue, May 10, 2011 at 10:25 AM, Irene Zweimueller
wrote:
> Dear list,
>
> I=B4m relatively new to Perl, so please be patient.
>
> I tied to get rid of whitespace characters by the following programme:
>
> my $i=3D" =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0USEFUL =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 ";
>
>
> if ($i =3D~ /\s/)
> {
> $i =3D~ s/\s*//;
> }
>
> print "xxxxxxxxxx $i xxxxxxxxxx\n";
>
> and it removes the whitespace characters in front of USEFUL, but not thos=
e
> behind..
> Any suggestions?
>
> kind regards
> Irene Zweimueller
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: gettting rid of whitespace characters

am 10.05.2011 20:48:57 von Shawn Wilson

On Tue, May 10, 2011 at 1:32 PM, Leo Susanto wrote:
> http://en.wikipedia.org/wiki/Trim_%28programming%29
>
> $string =3D~ s/^\s+|\s+$//g ; =A0 =A0 # remove both leading and trailing =
whitespace
>

1. don't do that. search on perlmonks and you'll find a long
discussion with benchmarks. that takes quite a bit longer and isn't
much more typing than separating them out.

though, i don't know whether putting the regex in an array and doing
$string ~~ @reg might be as slow as that.

2. don't top post - i cut out the rest of the conversation because i
had no other way to respond inline.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: gettting rid of whitespace characters

am 10.05.2011 22:17:08 von jwkrahn

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=3D" USEFUL ";
>
>
> if ($i =3D~ /\s/)
> {
> $i =3D~ s/\s*//;
> }

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

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

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

> print "xxxxxxxxxx $i xxxxxxxxxx\n";



John
--=20
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

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/