Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: validating file with perl

by space_monk (Chaplain)
on Jul 17, 2013 at 05:47 UTC ( #1044726=note: print w/ replies, xml ) Need Help??


in reply to validating file with perl

Sigh, I go away for a few weeks and see lots of solutions which don't use Text::CSV and their friends. Text::CSV can handle space or tab separated data as well as "normal" comma separated data.

Please do not succumb to the Dark Side, and use the Force of existing CPAN modules properly. :-)

use Text::CSV; my %data; my $csv = Text::CSV->new ( { binary => 1 } ) # should set binary attr +ibute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!"; while ( my $row = $csv->getline( $fh ) ) { my ($key, $value) = @$row; $data{$key} =0 if (!exists $data{$key}); $data{$key} |=1 if ($value == 1); $data{$key} |=2 if ($value == 3); } $csv->eof or $csv->error_diag(); close $fh; foreach my $key (%data) { print "$key invalid\n" if ($data{$key} != 3); }
If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)


Comment on Re: validating file with perl
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-08-28 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls