Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: TRIM in Perl?

by samurai (Monk)
on Sep 10, 2002 at 15:36 UTC ( [id://196722]=note: print w/replies, xml ) Need Help??


in reply to TRIM in Perl?

No, that will NOT drop the leading and trailing spaces. Only the leading spaces (you are anchored by "^").

There's no sub for trimming strings in perl. The best way is probably to just go ahead and bite the bullet:

my $temp = " Hello "; $temp =~ s/^\s+//; $temp =~ s/\s+$//;

You can write your own trim() function to automate this if you wish.

--
perl: code of the samurai

Replies are listed 'Best First'.
Re^2: TRIM in Perl?
by Aristotle (Chancellor) on Sep 10, 2002 at 16:21 UTC
    Occasionally also written as s/^\s+|\s+$//g;

    Makeshifts last the longest.

      I did some benchmarking, and I am absopositively amazed at how much faster that is than two s//'s. 3-4 times faster in my tests. Thank you very much for that one.

      --
      perl: code of the samurai

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found