Remove leading and trailing white space from a string

KevinADC KevinADC is offline Offline Sep 4th, 2006, 3:28 am |
0
This is a routine task best done with two regexp's when using perl.
Quick reply to this message  
Perl Syntax
  1. my $string = ' Mary had a little lamb. ';
  2. $string =~ s/^\s+//; #remove leading spaces
  3. $string =~ s/\s+$//; #remove trailing spaces
  4. print $string;
0
sut sut is offline Offline | Oct 3rd, 2006
Or:

$string =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
 
0
KevinADC KevinADC is offline Offline | Oct 4th, 2006
Yes, but benchmarking the two differnt ways is revealing:


  1. my $string = " Mary had a little lamb. ";
  2.  
  3. my $results = timethese(200000,
  4. {
  5. 'First' => sub {$string =~ s/^\s*//;$string =~ s/\s*$//;},
  6. 'Second' => sub {$string =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;}
  7. },
  8. );
  9. cmpthese( $results ) ;

results:


  1. Benchmark: timing 200000 iterations of First, Second...
  2. First: 1 wallclock secs ( 2.25 usr + 0.00 sys = 2.25 CPU) @ 88888.89/s (n=200000)
  3. Second: 3 wallclock secs ( 3.73 usr + 0.00 sys = 3.73 CPU) @ 53619.30/s (n=200000)
  4. Rate Second First
  5. Second 53619/s -- -40%
  6. First 88889/s 66% --

using two regexps is 66% percent faster (on my computer, results will vary).
 
0
rockslammer rockslammer is offline Offline | May 14th, 2008
$string =~ s/\s+$//; #remove trailing spaces
Worked very nicely for me. The expresion solved an LWP issue -- thanks
 
1
foobie foobie is offline Offline | Nov 28th, 2008
Actually, $string =~ s/^\s+//;$string =~ s/\s+$//; is faster, matching only when there is one or more whitespace characters.

Your Benchmark is flawed and only strips whitespace on the very first run - $string is global.

Try this benchmark instead:
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use Benchmark qw(cmpthese timethese);
  7.  
  8. sub double_star {
  9. my $string = shift;
  10. $string =~ s/^\s*//;
  11. $string =~ s/\s*$//;
  12. return $string;
  13. }
  14.  
  15. sub double_plus {
  16. my $string = shift;
  17. $string =~ s/^\s+//;
  18. $string =~ s/\s+$//;
  19. return $string;
  20. }
  21.  
  22. sub replace {
  23. my $string = shift;
  24. $string =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
  25. return $string;
  26. }
  27.  
  28. sub for_star {
  29. my $string = shift;
  30. for ($string) { s/^\s+//; s/\s+$//; }
  31. return $string;
  32. }
  33.  
  34. sub for_plus {
  35. my $string = shift;
  36. for ($string) { s/^\s*//; s/\s*$//; }
  37. return $string;
  38. }
  39.  
  40. sub regex_or {
  41. my $string = shift;
  42. $string =~ s/(?:^ +)||(?: +$)//g;
  43. return $string;
  44. }
  45.  
  46. cmpthese(
  47. -1,
  48. {
  49. 'double_star' => q|double_star(' Mary had a little lamb. ');|,
  50. 'double_plus' => q|double_plus(' Mary had a little lamb. ');|,
  51. 'replace' => q|replace( ' Mary had a little lamb. ');|,
  52. 'for_star' => q|for_star( ' Mary had a little lamb. ');|,
  53. 'for_plus' => q|for_plus( ' Mary had a little lamb. ');|,
  54. 'regex_or' => q|regex_or( ' Mary had a little lamb. ');|,
  55. }
  56. );

Results:
  1. Rate regex_or replace for_plus double_star for_star double_plus
  2. regex_or 55855/s -- -47% -49% -60% -73% -84%
  3. replace 105217/s 88% -- -5% -25% -49% -70%
  4. for_star 110277/s 97% 5% -- -22% -46% -68%
  5. double_star 140894/s 152% 34% 28% -- -31% -59%
  6. for_plus 204799/s 267% 95% 86% 45% -- -41%
  7. double_plus 345717/s 519% 229% 213% 145% 69% --
 
0
KevinADC KevinADC is offline Offline | Dec 10th, 2008
foobie,

excellent post and a good observation about my flawed test.

Regards,
Kevin
 
0
acca acca is offline Offline | 28 Days Ago
Another one:

sub chomp_plus {
my $string=shift;
$string =~ s/^\s+//;
chomp $string;
return $string;
}
  1. Rate regex_or replace for_plus double_star for_star double_plus chomp_plus
  2. regex_or 98642/s -- -50% -55% -63% -71% -81% -88%
  3. replace 196495/s 99% -- -11% -26% -42% -63% -77%
  4. for_plus 220554/s 124% 12% -- -17% -35% -58% -74%
  5. double_star 265481/s 169% 35% 20% -- -22% -50% -69%
  6. for_star 341333/s 246% 74% 55% 29% -- -35% -60%
  7. double_plus 526091/s 433% 168% 139% 98% 54% -- -38%
  8. chomp_plus 849541/s 761% 332% 285% 220% 149% 61% --
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC