Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2015-10-07 20:48 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (200 votes), past polls