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+$//;
| [reply] [d/l] |
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.
| [reply] [d/l] |
Re: removing the white spaces
by Zaxo (Archbishop) on Apr 13, 2006 at 15:26 UTC
|
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.
| [reply] [d/l] |
|
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).
| [reply] [d/l] [select] |
Re: removing the white spaces
by davidrw (Prior) on Apr 13, 2006 at 13:19 UTC
|
$text =~ s/(^\s+|\s+$)//g
| [reply] [d/l] |
|
RESIST THE URGE! Seriously, don't do that. It slows the whole thing down like you wouldn't believe.
| [reply] |
|
It does? Why's that, then?
| [reply] |
|
|
A noncapturing solution should speed things.
untested:
s/(?:^\s+)|(?:\s+$)//g
| [reply] [d/l] |
|
|
| [reply] |
Re: removing the white spaces
by Roy Johnson (Monsignor) on Apr 13, 2006 at 21:08 UTC
|
| [reply] |
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% -
+-
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
$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% --
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |