I have some uncomplicated CSV records to parse and thought the split function would be just fine to use. But I have encountered some oddities that I cannot find an explanation for...
here is the first example:
my $test = "25/May/2011 16:19:02,0,0,4,0.04,5,1,0.08,8.5,,,,,,,,"; my @test = split /,/, $test; foreach (@test){printf "!%s!\n", $_;}
which yields: !25/May/2011 16:19:02! !0! !0! !4! !0.04! !5! !1! !0.08! !8.5! Now if I change the code to:
my $test = "25/May/2011 16:19:02,0,0,4,0.04,5,1,0.08,8.5,,,,,,,,4"; my @test = split /,/, $test; foreach (@test){printf "!%s!\n", $_;}
I now get... !25/May/2011 16:19:02! !0! !0! !4! !0.04! !5! !1! !0.08! !8.5! !! !! !! !! !! !! !! !4! And finally, with a trailing comma:
my $test = "25/May/2011 16:19:02,0,0,4,0.04,5,1,0.08,8.5,,,,,,,,4,"; my @test = split /,/, $test; foreach (@test){printf "!%s!\n", $_;}
The results are: !25/May/2011 16:19:02! !0! !0! !4! !0.04! !5! !1! !0.08! !8.5! !! !! !! !! !! !! !! !4!
So my question is how do I get split to recognize the "trailing" empty fields of the string, when there is only one or multiple, if at all possible?
thanks, gary
In reply to split ignores "fields" by conversecorollary
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |