Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: what is the function should I need to use for trim white spaces ?

by GrandFather (Saint)
on Jan 11, 2006 at 19:25 UTC ( [id://522538]=note: print w/replies, xml ) Need Help??


in reply to what is the function should I need to use for trim white spaces ?

I tend to do it:

use strict; use warnings; my $before = " some text "; ($_ = $before) =~ s/^\s+|\s+$//g; print ">$before<\n"; print "|$_|\n";

Prints:

> some text < |some text|

I guess this is sufficiently light weight that it is thought a function is not required. Does seem a slight omission though.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: what is the function should I need to use for trim white spaces ?
by ikegami (Patriarch) on Jan 11, 2006 at 19:32 UTC

    Assinging to $_ without localizing it is dangerous.
    (local $_ = $before) =~ s/^\s+|\s+$//g;
    or
    (my $after = $before) =~ s/^\s+|\s+$//g;
    should be used in just almost all cicumstances.

Re^2: what is the function should I need to use for trim white spaces ?
by rational_icthus (Sexton) on Jan 11, 2006 at 19:52 UTC
    I think the alternation in the s/^\s+|\s+$// version causes significant time costs in large applications. I often work with tab-delimited files containing hundreds of thousands of lines. If I'm tab-splitting these lines and then trimming each one, I'm going to pick the double-regex approach each time. I wrote some quick code that benchmarked the double-regex vs single-regex approach against three strings.
    use Benchmark; my @words = ('trim_unneeded',' front trim only','rear trim only ',' + both side trim '); for my $word (@words){ print "Benchmarking $word...\n\n"; timethese(1_000_000, {double => sub{ $word =~ s/^\s+//; $word =~ s +/\s+$//; }, single => sub{ $word =~ s/^\s+|\s+$//; }}) }
    The code was run on a Celeron D 2.8 GHz machine running XP with the following results:

    'trim_unneeded'
    Single Regex: 0.45 seconds
    Double Regex: 2.27 seconds

    ' front trim only'
    Single Regex: 0.67 seconds
    Double Regex: 2.66 seconds

    'rear trim only '
    Single Regex: 0.67 seconds
    Double Regex: 2.45 seconds

    ' both side trim '
    Single Regex: 0.66 seconds
    Double Regex: 2.44 seconds

    That's after only 1,000,000 trims. In a 800,000 line file with 50 columns per line, we're talking about 40,000,000 trims. Assuming a linear scale, that means I give up about a minute of processing time per file per run. That's far less than the time it would have taken me to type two regexes. Admittedly, it's a small optimization, and only valid for those who are processing files on the scale that I do, but for most people who end up typing the 'trim regex' often enough to complain about it on perlmonks, it probably applies.
      "I think the alternation in the s/^\s+|\s+$// version causes significant time costs in large applications."

      Wait, isn't the single regex faster in your benchmark?

      "Single Regex: 0.45 seconds
      Double Regex: 2.27 seconds"

      Ordinary morality is for ordinary people. -- Aleister Crowley
Re^2: what is the function should I need to use for trim white spaces ?
by blazar (Canon) on Jan 12, 2006 at 11:00 UTC
    ($_ = $before) =~ s/^\s+|\s+$//g;

    Just as a minor nitpick I'd localise $_, but then I wouldn't bind with =~, I'd just

    local $_ = $before; s/^\s+|\s+$//g;

    or else

    (my $after = $before) =~ s/^\s+|\s+$//g;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-24 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found