http://www.perlmonks.org?node_id=1044726


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! :-)