Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Split Trailing Nulls

by p6steve (Sexton)
on Feb 07, 2004 at 12:31 UTC ( #327309=perlquestion: print w/replies, xml ) Need Help??

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

In my quest to beautify code ... is there a more elegant way to split a csv with trailing nulls into a 2D array than the following?
#Read all transactions my $lref; my $line = ''; my @fields = (); my @alltrans = (); open( ALL, "<alltrans.csv" ) or die "can't open alltrans.csv"; while( <ALL> ) { chomp; $line = $_ . ' '; #ugly as only copes with one trailing null fi +eld @fields = split( ',', $line ); $fields[-1] = ( $fields[-1] eq ' ' ) ? '' : $fields[-1]; $lref = [ @fields ]; push( @alltrans, $lref ); } close ALL; print Dumper @alltrans;

Replies are listed 'Best First'.
Re: Split Trailing Nulls
by delirium (Chaplain) on Feb 07, 2004 at 15:00 UTC
    Most CSV-related posts end up with an obligatory reference to Text::CSV or a similar module, and a reminder about the CSV format. Here's mine:

    If your data is really CSV data, don't forget to take into account the possibility of bizarre quoted constructs, such as
    Field 1,"""This is quoted,"" and this isn't", Field 3

    Splitting on a comma with real CSV data is unsafe. Consider a module such as the above link, or use another method of dividing the fields. Here's mine, which is ugly but workable:

    sub csv_split { local $_ = shift || return undef; my @array = (); my $count = my $quoted = 0; while ( s/(.)// ) { my $char = $1; if ($char eq ',' && !$quoted) { $count++; next; } if ($char eq q/"/) { unless ( $quoted && s/^\"// ) { $quoted = 1 - $quoted; nex +t; } } $array[$count] .= $char; } return @array; }
Re: Split Trailing Nulls
by Roger (Parson) on Feb 07, 2004 at 13:25 UTC
    You can read about split by typing:
    perldoc -f split

    Set the LIMIT of split to -1 to preserve trailing nulls.
    open ALL, "alltrans.csv" or die "Can not open file: $!"; while (<ALL>) { chomp; push @alltrans, [ split /,/, $_, -1 ]; } close ALL;

    I would even write it as a one-liner, but that's a matter of personal taste:
    my @alltrans; chomp, push @alltrans, [ split /,/, $_, -1 ] for <ALL>; # or my @alltrans = map { chomp; [ split /,/, $_, -1 ] } <DATA>;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://327309]
Approved by b10m
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2022-05-18 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (70 votes). Check out past polls.

    Notices?