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

Hi, I have this code:
perl -MData::Dumper -e ' $str = " STRING "; @ar = split(/\s+/,$str); print Dumper \@ar;'
Why my code above gives:
$VAR1 = [ '', 'STRING' ];
But I want simply this:
$VAR1 = [ 'STRING' ];
What's wrong with my regex above? Thanks so much.

Replies are listed 'Best First'.
Re: Nagging space after splitting
by Zaxo (Archbishop) on May 31, 2005 at 06:16 UTC

    The presence of initial spaces forces the empty string as the first element. That effect is suppressed for trailing match, but the initial match is regarded an a separator.

    The magical split trims leading and trailing whitespace, so is probably what you want:

    $ perl -MData::Dumper -e ' > $str = " STRING "; > @ar = split(" ", $str); > print Dumper \@ar;' $VAR1 = [ 'STRING' ]; $

    After Compline,
    Zaxo

Re: Nagging space after splitting
by ivancho (Hermit) on May 31, 2005 at 06:18 UTC
    from  perldoc -f split

    As a special case, specifying a PATTERN of space (' ') will split on white space just as "split" with no arguments does. Thus, "split(' ')" can be used to emulate awk's default behavior, whereas "split(/ /)" will give you as many null initial fields as there are leading spaces. A "split" on "/\s+/" is like a "split(' ')" except that any leading whitespace produces a null first field. A "split" with no arguments really does a "split(' ', $_)" internally.

Re: Nagging space after splitting
by dbwiz (Curate) on May 31, 2005 at 06:57 UTC
Re: Nagging space after splitting
by tlm (Prior) on May 31, 2005 at 06:19 UTC

    Your regex is fine. split returns the stuff around the regex; in this case, the resulting first element is the empty string before the regex. But you can obtain the desired behavior by using the special first argument ' ' for split:

    my @ar = split( ' ', $str );

    the lowliest monk

Re: Nagging space after splitting
by ikegami (Patriarch) on May 31, 2005 at 07:02 UTC

    If you were splitting on something other than whitespace (and thus couldn't use " " for the pattern), you could do:

    @ar = split(/$pattern/, $str); shift(@ar);

    or

    $str =~ s/^$pattern//; @ar = split(/$pattern/, $str);

    If you knew the number of fields, you could also use:

    @ar = (split(/$pattern/, $str))[1..$n-1];
Re: Nagging space after splitting
by tphyahoo (Vicar) on May 31, 2005 at 08:00 UTC
    Look at it another way.
    perl -MData::Dumper -e ' $str = ",STRING,"; @ar = split(/,/,$str); print Dumper \@ar;'
    should also give you an empty first field, right? Maybe that demo helps make this behavior more understandable.
Re: Nagging space after splitting
by tchatzi (Acolyte) on May 31, 2005 at 07:41 UTC
    In your case it will work like this
    # perl -MData::Dumper -e ' > $str = " STRING "; > $str =~ s/\s//g; > push(@ar,$str); > print Dumper \@ar;'
    You will get
    $VAR1 = [ 'STRING' ];
    Is this what you want?
    But if you want to use split then use it as Zaxo told you

    ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI