Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

removing the white spaces

by Anonymous Monk
on Apr 13, 2006 at 13:12 UTC ( [id://543066]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks

i would like to know the method to remove the spaces outside the text.
for example,
$text=" The perl ";
i should get output as "The   perl"

Edited by planetscape - added code tags

Replies are listed 'Best First'.
Re: removing the white spaces
by derby (Abbot) on Apr 13, 2006 at 13:16 UTC

    $text = " The perl "; # remove leading whitespace $text =~ s/^\s+//; # remove trailing whitespace $text =~ s/\s+$//;

    -derby
Re: removing the white spaces
by Herkum (Parson) on Apr 13, 2006 at 13:19 UTC
    $text =~ s{ # Substitute \A # from the beginning of the string \s+ # one or more white space characters }{}xms; # with nothing $text =~ s{ # Substitute \s+ # one or more white space characters \z # from the end of the string }{}xms; # with nothing $text =~ s{ # Substitute \s+ # one or more white space characters }{ }gxms; # with one space, anywhere in the string # As many times as it occurs

    Modified my code to address multiple white spaces in the middle of the string and remove white space from the front of the string. This is a little more verbose than the previous poster but I tried to comment each piece to explain it better.

Re: removing the white spaces
by Zaxo (Archbishop) on Apr 13, 2006 at 15:26 UTC

    Here's an idiomatic way,

    my $text = ' The perl '; $text = join ' ', split ' ', $text;
    That problem is called "normalizing whitespace". This solution depends on the whitespace-devouring nature of magical split.

    After Compline,
    Zaxo

      If you view the OP's HTML source, you'll see he actually asked:

      i should get output as "The   perl"

      and not

      i should get output as "The perl"

      Your solution doesn't answer his question (although that's not your fault).

Re: removing the white spaces
by davidrw (Prior) on Apr 13, 2006 at 13:19 UTC
    or combined into one:
    $text =~ s/(^\s+|\s+$)//g
      RESIST THE URGE! Seriously, don't do that. It slows the whole thing down like you wouldn't believe.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        A noncapturing solution should speed things.
        untested:
        s/(?:^\s+)|(?:\s+$)//g
      thanks monks.
Re: removing the white spaces
by Roy Johnson (Monsignor) on Apr 13, 2006 at 21:08 UTC
Re: removing the white spaces
by pKai (Priest) on Apr 13, 2006 at 20:27 UTC

    Nice, never occurred to me to use join split for that task.

    Though using s/// seems to be noticeable faster, at least if not used with an alternating regex:


    Update, in reaction to ikegami's reply:

    My above code is seriously flawed. Not only the unprovoked change from + to *, but moreso in forgetting the mandatory g modifier in the regex with alternation which should trim the start and the end of the argument string!

    Should have told me something, that the version with the worse reputation scored best!

    So here comes my corrected and expanded try:

    Which gives on this machine:

    Short string: Rate 2-s 2-sp 2-sp_r 3-s 3-sp_r 3-sp j-spl +it 2-s 114468/s -- -14% -26% -28% -42% -47% -4 +7% 2-sp 132675/s 16% -- -14% -17% -33% -38% -3 +8% 2-sp_r 155014/s 35% 17% -- -3% -21% -28% -2 +8% 3-s 159352/s 39% 20% 3% -- -19% -26% -2 +6% 3-sp_r 196609/s 72% 48% 27% 23% -- -8% - +9% 3-sp 214344/s 87% 62% 38% 35% 9% -- - +0% j-split 214880/s 88% 62% 39% 35% 9% 0% +-- Long string: Rate 2-s 2-sp 3-s j-split 2-sp_r 3-sp 3-sp_ +r 2-s 1961/s -- -3% -37% -76% -77% -83% -85 +% 2-sp 2029/s 3% -- -34% -76% -76% -83% -85 +% 3-s 3092/s 58% 52% -- -63% -63% -74% -77 +% j-split 8343/s 325% 311% 170% -- -1% -29% -38 +% 2-sp_r 8443/s 331% 316% 173% 1% -- -28% -38 +% 3-sp 11694/s 496% 476% 278% 40% 39% -- -13 +% 3-sp_r 13512/s 589% 566% 337% 62% 60% 16% - +-

      Why did you convert the "+"s to "*"s? That slows things down greatly (24% for $short, 74% for $long).

      Rate triple-s triple-sp triple-s 122303/s -- -24% triple-sp 160627/s 31% -- Rate triple-s triple-sp triple-s 2337/s -- -74% triple-sp 8991/s 285% --

      That surprised me, honestly. I only expected it to help when there were no leading/trailing spaces.

      You can probably speed up the "_r" versions a little by removing unnecessary "+"s. For example, change

      $str =~ s/\s+/ /g; $str =~ s/^\s+//; $str =~ s/\s+$//;
      to
      $str =~ s/\s+/ /g; $str =~ s/^\s//; $str =~ s/\s$//;

      Update: The improvement is quite substantial. Without the +, it ties 3-sp for fastest for short strings, and it's 42% faster than second fastest for long strings.

      Short string: Rate 3-sp_r 3-sp_r2 3-sp 3-sp_r 329182/s -- -14% -14% 3-sp_r2 382305/s 16% -- -1% 3-sp 384541/s 17% 1% -- Long string: Rate 3-sp 3-sp_r 3-sp_r2 3-sp 20911/s -- -12% -38% 3-sp_r 23687/s 13% -- -29% 3-sp_r2 33582/s 61% 42% --
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://543066]
Approved by jesuashok
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found