Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: How to get split $var to work like split ' '?

by LanX (Bishop)
on Sep 10, 2013 at 15:58 UTC ( #1053306=note: print w/replies, xml ) Need Help??

in reply to How to get split $var to work like split ' '?

I can only guess which default behavior you want, b/c you didn't make it clear.

If you want repeated whitespaces to be ignored, then simply tell split to do so.

DB<113> split ' ', 'abc def' => ("abc", "def") DB<114> $del=qr/ / => qr/ / DB<115> split $del, 'abc def' => ("abc", "", "def") DB<116> $del=qr/\s+/ => qr/\s+/ DB<117> split $del, 'abc def' => ("abc", "def")

Cheers Rolf

( addicted to the Perl Programming Language)


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 nu +ll 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.

Replies are listed 'Best First'.
Re^2: How to get split $var to work like split ' '?
by QM (Parson) on Sep 10, 2013 at 16:31 UTC
    I'd like the split ' ' behavior, unless changed from the command line option. Something like this:
    my $split_pattern = ' '; # default ... # $split_pattern might get changed here from a command line option $split_pattern = $foo if $bar; ... $line = " one two three \n"; my @words = split $split_pattern, $line; for my $i (0..$#words) { print "$i: ($words[$i])\n"; } # Expected output for default case 0: one 1: two 2: three # Actual output for default case 0: () 1: () 2: () 3: (one) 4: (two) 5: (three) 6: () 7: () 8: ( )

    Quantum Mechanics: The dreams stuff is made of

      Now, I understand what you are looking for, it wasn't clear to me so far. If you store ' ' into a variable, you don't get the expected behavior as if you hard code:

      my @fields = split ' ', $line;

      Using "qr/\s+/;" as a split pattern improves the result but still does not yields what you want (you still get an extra empty element at the beginning of the array).

      Well, then I don't see any real simple direct solution. Even something like this:

      my @fields = split $pattern? $pattern : ' ', $line;

      does not do what you are looking for. The only solutions I can think of are either to use the above "qr/\s+/;" and shift the first element if empty, or use a if else construct, or yet this equivalent construct:

      my @fields  = defined $pattern? split $pattern, $line : split ' ', $line;
Re^2: How to get split $var to work like split ' '?
by hdb (Monsignor) on Sep 10, 2013 at 20:30 UTC

    Many thanks. I had not seen that bit.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053306]
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2018-04-26 08:56 GMT
Find Nodes?
    Voting Booth?