Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: lightweight CSV parser

by wrinkles (Pilgrim)
on Dec 22, 2011 at 20:00 UTC ( #944835=note: print w/ replies, xml ) Need Help??


in reply to Re: lightweight CSV parser
in thread lightweight CSV parser

Thanks Tux, I converted my split functions to use Text::CSV. Here's the new and much-improved code:

sub _moredata_array { my ($datastring, $datasep_cfg) = @_; use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char = +> "$datasep_cfg" }); my $io; open ($io, "<:encoding(utf8)", \$datastring) or die "Cannot use CSV: + $!".Text::CSV->error_diag (); my $row = $csv->getline ($io); $row || undef; } sub _moredata_hash { my ($datastring, $hashsep) = @_; use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char = +> "$hashsep" }); my $io; open ($io, "<:encoding(utf8)", \$datastring) or die "Cannot use CSV: + $!".Text::CSV->error_diag (); my %hash; while (my $colref = $csv->getline($io)) { my $count = scalar @{$colref}; die "$count is an odd number of keys and values at @{$colref}.\n" +if ($count%2); $hash{$colref->[0]} = $colref->[1]; } scalar %hash ? \%hash : undef; }


Comment on Re^2: lightweight CSV parser
Download Code
Re^3: lightweight CSV parser
by Tux (Monsignor) on Dec 23, 2011 at 09:17 UTC

    As you asked for generic comment in your OP, a few remarks

    # modules are usually not "use"d inside subs, but just once at the cod +e start use Text::CSV; sub _moredata_array { my ($datastring, $datasep_cfg) = @_; # Do not/never quote variables if you do not explicitly want to fo +rce PV context # e.g. if $datasep_cfg is undefined is means something completely +different then # the empty string which it is forced to when quoted my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char +=> $datasep_cfg }); # you can declare $io as lexical (my) in the open call itself # At this point,Text::CSV->error_diag () is undefined, as $csv has + not even been used # and opening of the scalar as file handle does not interact with +$csv (yet) open my $io, "<:encoding(utf8)", \$datastring or die "Cannot use C +SV: $!"; # no need to declare a variable # the return value can possible be a reference to an empty list. t +hink if you want # that. || undef is useless here return $csv->getline ($io); } sub _moredata_hash { my ($datastring, $hashsep) = @_; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char +=> $hashsep }); open my $io, "<:encoding(utf8)", \$datastring or die "Cannot use C +SV: $!"; my %hash; while (my $row = $csv->getline($io)) { my $count = scalar @{$row}; $count % 2 and die "$count is an odd number of keys and values + at (@{$row}).\n"; $hash{$row->[0]} = $row->[1]; } scalar %hash ? \%hash : undef; }

    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://944835]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-12-22 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls