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

Hi Monks,
$a="a,b,c,d," @col = split(",",$a);
I want to use split to generate the array ('a','b','c','d','')--notice the extra character at the end. Do I really have to check for the number of columns after the split and push on an extra character at the end. Is there any way to do this within split using regular expressions? Many thanks as always.

Replies are listed 'Best First'.
Re: I cannot believe I'm asking a question about split
by Cristoforo (Curate) on Aug 08, 2012 at 22:07 UTC
    From the documentation for split:
    1. print join(':', split(',', 'a,b,c,,,')), "\n"; produces the output 'a:b:c', but the following: 1. print join(':', split(',', 'a,b,c,,,', -1)), "\n"; produces the output 'a:b:c:::'.

    Chris

      Many many thanks Chris.. That's exactly what I was looking for!
Re: I cannot believe I'm asking a question about split
by eyepopslikeamosquito (Archbishop) on Aug 08, 2012 at 22:15 UTC
    By default, split preserves leading empty fields but strips trailing empty fields. You can change that behavior, and preserve trailing empty fields, by using -1 as the last parameter, like so:
    @col = split(",",$a,-1);
    See also: FMTYEWTK about split //

Re: I cannot believe I'm asking a question about split
by kcott (Archbishop) on Aug 08, 2012 at 22:15 UTC

    Use a LIMIT of -1. It's documented in split.

    $ perl -Mstrict -Mwarnings -E ' my $x = q{a,b,c,d,}; my @col = split /,/, $x, -1; say "X${_}X" for @col; ' XaX XbX XcX XdX XX

    -- Ken

Re: I cannot believe I'm asking a question about split
by Kenosis (Priest) on Aug 08, 2012 at 22:26 UTC

    split does use a regex, as the second parameter passed to it. That being said, perhaps a regex with a capture will effectively propagate an array for you, sans the 'extra characters:'

    use Modern::Perl; use Data::Dumper; my $a = 'a,b,c,d,,,,,,,'; my @col = $a =~ /([^,]+)/gi; say Dumper \@col;

    Output:

    $VAR1 = [ 'a', 'b', 'c', 'd' ];

    However, if you know the number of columns and want to limit split to those, you can do the following:

    use Modern::Perl; use Data::Dumper; my $a = 'a,b,c,d,,,,,,,'; my @col = ( split ',', $a, )[ 0 .. 3 ]; say Dumper \@col;

    Output:

    $VAR1 = [ 'a', 'b', 'c', 'd' ];

    Hope this helps!

    Update: my @col = split /,+/, $a; produces the same output as both examples above.