Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: trimming space from both sides of a string

by mr_mischief (Monsignor)
on Oct 14, 2010 at 20:10 UTC ( [id://865344]=note: print w/replies, xml ) Need Help??


in reply to trimming space from both sides of a string

Updated. Fixed based on the error GrandFather was kind enough to point out.

This reminds me of a discussion of the very thing from one of the newsgroups. It was almost certainly comp.lang.perl.misc in which it took place, about a decade ago. It included silly benchmarks. Yes, we're talking premature optimization of a simple line or two not likely to be a critical point and all that. I seem to remember that s/^\s.*//; s/\s.*$//; s/^\s*//; s/\s*$//; both did very well in the benchmarks and was accepted pretty well as clear over clever. It beat out things involving extra interpolations, substr, etc.

Now, let me leverage Google Groups to refresh my memory and see if the above conclusion I recall was right. It might be interesting (although still somewhat silly) to see how the benchmarks compare on more modern perls compared to what we had then.

Here's one such: NEED: Fast, Fast string trim(). Here's another: Removing spaces in a string. It turns out there are several such threads in the newsgroup on this topic between 1996 and 2002, with some later than that. Moreover, more than one went so far as to include benchmarks.

This obviously doesn't prove the strange ones ever made it into larger projects, but given there were proponents for strange constructions there's a chance.

Replies are listed 'Best First'.
Re^2: trimming space from both sides of a string
by GrandFather (Saint) on Oct 16, 2010 at 06:20 UTC

    It seems likely (without benchmarking) that those two substitutions (s/^\s.*//; s/\s.*$//;) are very efficient at removing leading and trailing white space. However the side effect of removing everything if there is leading or trailing white space may be too much of a price to pay for "efficiency"!

    True laziness is hard work
      oh! Right! No dot in there. s/^\s*//; s/\s*$//; instead of course. s/^\s+//; s/\s+$//; should work as well.

        s/^\s+//; s/\s+$//; works better than s/^\s*//; s/\s*$//;.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found