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

I have an array with phrases and I need to chomp all leading and trailing whitespace.

I tried

foreach my $asd (@phrase){ chomp($asd); print "$asd\n"; }
It prints things out like
hi there this and that and the other thing blah blah blah one two there four five six
The first line appears right but all others still have a leading space.

Replies are listed 'Best First'.
Re: chomp not working in array
by Fletch (Bishop) on Nov 15, 2004 at 21:16 UTC

    As has been mentioned, chomp only removes $/ from the end of things. See also perldoc -q "blank space"

    Found in /System/Library/Perl/5.8.1/pods/perlfaq4.pod How do I strip blank space from the beginning/end of a string? Although the simplest approach would seem to be $string =~ s/^\s*(.*?)\s*$/$1/; not only is this unnecessarily slow and destructive, it also fa +ils with embedded newlines. It is much faster to do this operation in t +wo steps: $string =~ s/^\s+//; $string =~ s/\s+$//; Or more nicely written as: for ($string) { s/^\s+//; s/\s+$//; } This idiom takes advantage of the "foreach" loop's aliasing beh +avior to factor out common code. You can do this on several strings at +once, or arrays, or even the values of a hash if you use a slice: # trim whitespace in the scalar, the array, # and all the values in the hash foreach ($scalar, @array, @hash{keys %hash}) { s/^\s+//; s/\s+$//; }
Re: chomp not working in array
by Ovid (Cardinal) on Nov 15, 2004 at 21:04 UTC

    chomp removes the trailing newline (or the line ending represented by $/, if you wish to be accurate.) I think what you're looking for is a trim() function that removes leading and possibly trailing whitespace?

    Cheers,
    Ovid

    New address of my CGI Course.

Re: chomp not working in array
by eric256 (Parson) on Nov 15, 2004 at 21:22 UTC

    Chomp is not the tool of choice here. You could however use a couple of regex to do the job

    use strict; use warnings; my @phrases = ("hi there", " this and that ", " and the other thing", "blah blah blah ", "one two there", " four five six "); @phrases = map { trim($_) } @phrases; print join("\n", @phrases); sub trim { my $string = shift; $string =~ s/^\s+//; #trim leading space $string =~ s/\s+$//; #trim trailing space return $string; }

    You will still want to use chomp to get rid of line endings, and perhaps modify trim to take an array and trim it, instead of individual strings. I'm not sure why perl doesn't include a builtin trim, perhaps they just figure its one of those "build to your own need" type tools.


    ___________
    Eric Hodges
Re: chomp not working in array
by Roy Johnson (Monsignor) on Nov 15, 2004 at 22:09 UTC
Re: chomp not working in array
by larryp (Deacon) on Nov 15, 2004 at 22:42 UTC

    Try this instead:

    #!/usr/bin/perl use strict; use warnings; my @lines = <DATA>; # Don't do this unless necessary. :) chomp( @lines ); # Removes IRSs from all lines. # Loops through array, pulls out a line, trims white space from front # and back and prints the line. foreach my $line ( @lines ) { $line = trimSpaces( $line ); print $line, "\n"; } # The regular expression in the subroutine looks for zero # or more space characters at the beginning and end of the # line and removes them. sub trimSpaces() { my $value = shift( @_ ); $value =~ s/(^\s*|\s*$)//g; return $value; } __DATA__ hi there this and that and the other thing blah blah blah one two there four five six

    The 'chomp' command is doing exactly what it's supposed to. It's removing the Input Record Separator (the contents of the builtin $/ variable.) If you haven't set this explicitly, the value is dictated by your operating system. You may be thinking of the 'chop' command. However, I encourage you to use care with chop as it removes the trailing character, no matter what it is. :)

    HTH,

    /Larry

Re: chomp not working in array
by ercparker (Hermit) on Nov 16, 2004 at 06:20 UTC
    Assuming its just leading and trailing spaces you can also do this:
    my @array = ( 'hi there ', ' this and that ', ' and the other thing ', ' blah blah blah ', ' one two there ', ' four five six '); map { s[(^\s+|\s+$)][]g }@array;
Re: chomp not working in array
by TedPride (Priest) on Nov 16, 2004 at 14:56 UTC
    map { s/(?:^\s+|\s+$)//g; print "$_\n"; } @phrase;
    Don't forget he wanted the lines printed too, and since you don't need the found values returned, you can add a ?: flag at the start.
A reply falls below the community's threshold of quality. You may see it by logging in.