Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

TRIM in Perl?

by peacemaker1820 (Pilgrim)
on Sep 10, 2002 at 14:00 UTC ( [id://196673]=perlquestion: print w/replies, xml ) Need Help??

peacemaker1820 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks.
I would like to trim some words in my data file from the leading and trailing spaces. Let's say if I have:
$temp = " Hello ";
after doing this,
$temp =~ s/^\s+//;
that will drop all the leading and trailing spaces
This will certainly solve my problem, but I was wandering if there is a built in SUB for strings in Perl that does the same job?
Thanks.

Replies are listed 'Best First'.
Re: TRIM in Perl?
by jmcnamara (Monsignor) on Sep 10, 2002 at 14:05 UTC
Re: TRIM in Perl?
by samurai (Monk) on Sep 10, 2002 at 15:36 UTC
    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

      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

Re: TRIM in Perl?
by charnos (Friar) on Sep 10, 2002 at 16:31 UTC
    Since with all the suggestions here we're just eliminating all spaces in the string, wouldn't simple transliteration ($temp =~ tr/ //d;) be faster/simpler?

    Update: EEK! *bangs head* Nevermind, I retract that, $temp =~ s/^\s+|\s$/ would be the best way to do it.
      Where did you see "all suggestions [..] just eliminating all spaces"? None of them do.

      Makeshifts last the longest.

Re: TRIM in Perl?
by Rabenschwinge (Novice) on Jan 12, 2011 at 10:34 UTC

    I have a problem with that one:

    #!/usr/bin/env perl use strict; use warnings; my @strings = (' Test ', 'Test 2 ', ' A ', 'multi line string ',' '); foreach my $string (@strings) { $string =~ s/^\s+//m; $string =~ s/\s+$//m; print "'$string'\n"; }

    Returns:

    'Test' 'Test 2' 'A' 'multi line string' ''
    That is almost correct, but it does remove a line break in the middle of the string it shouldn't: There should be two line breaks between "multi" and "line" but only one remains.

      Why do you add /m to your regular expressions? See perlop - /m changes the meaning of $ and ^.

      That is almost correct, but it does remove a line break in the middle of the string it shouldn't

      Who says it shouldn't?

      The OP wanted whitespaces removed, that is what \s means, whitespaces (\n, \r, \t, \f, and " ")

      New questions go in Seekers of Perl Wisdom (see Where should I post X?) , don't worry, yours will be moved there shortly
      I wanted the same thing I think - remove leading and trailing space, on a per line basis, ignoring newlines. I've just done this and it seems to work, i.e. linecount remains same before and after.
      cat test.dat | perl -n -e 'chomp; s/^\s+//; s/\s+$//; print "$_\n";'

Log In?
Username:
Password:

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

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

    No recent polls found