Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Fooled By Split

by roho (Chancellor)
on Nov 24, 2003 at 11:59 UTC ( #309508=perlquestion: print w/replies, xml ) Need Help??

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

I used split to parse a simple comma-delimited text string (i.e. no embedded commas, no quoted values, etc) into an array variable and the last values in the text string were all null. It is important for my process to maintain the null (or undefined) entries in the array, however it appears that split does not create array entries (even undef entries) for trailing null entries in the text string. This is not at all the behavior I expected from split and I can find no way to work around it, other than appending a throw-away character to the end of the text string prior to splitting it, which seems awfully kludgey. I tried adding a limit number equal to the total number of entries I expected (including the trailing null entries), but it had no effect. Can anyone shed some light on split's behavior and/or suggest a cleaner work around? Thanks in advance. Sample code follows ...

#!/usr/bin/perl -w use strict; my $txt = 'a,b,c,d,,,,,,'; my @fld = split(/,/,$txt); print "\$txt = |$txt|\n"; print "\@fld = |@fld|\n"; for (0 .. $#fld) {print "\$fld\[", $_, "\] = |$fld[$_]|\n"}; __END__ Results: -------- $txt = |a,b,c,d,,,,,,| @fld = |a b c d| $fld[0] = |a| $fld[1] = |b| $fld[2] = |c| $fld[3] = |d|

"Its not how hard you work, its how much you get done."

Replies are listed 'Best First'.
Re: Fooled By Split
by Anonymous Monk on Nov 24, 2003 at 12:07 UTC

    my @fld = split(/,/,$txt, -1); will take you where you wanted to go.

Re: Fooled By Split
by broquaint (Abbot) on Nov 24, 2003 at 12:11 UTC
    Unfortunately this behaviour of stripping trailing null fields is inherent in split's operation (update: unless you take advantage of split's 3rd argument. ahem). An alternate solution would be to apply a capturing regex e.g
    use Data::Dumper; my $txt = 'a,b,c,d,,,,,,'; my @fld = $txt =~ /([^,]*)(?:,|$)/g; print Dumper \@fld; __output__ $VAR1 = [ 'a', 'b', 'c', 'd', '', '', '', '', '', '' ];
    So that regex captures everything between commas, even null fields. See. perle and perlop for more info.


Re: Fooled By Split
by roho (Chancellor) on Nov 24, 2003 at 12:11 UTC
    My bad ... boy do I feel foolish ... I reread the Camel book and there it was as plain as day ... use a negative limit value to keep split from dropping trailing nulls. Sorry about that. I should know better.

    "Its not how hard you work, its how much you get done."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://309508]
Approved by gmax
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2020-02-25 06:58 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (108 votes). Check out past polls.