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

Re: Diff CSV files - But ignore certain fields

by McA (Priest)
on Mar 04, 2013 at 16:50 UTC ( #1021675=note: print w/replies, xml ) Need Help??

in reply to Diff CSV files - But ignore certain fields


I give that as a starting point. Nr. 1 should be solved. the rest is an execise for tsk1979.

#!/usr/bin/perl use warnings; use strict; use Text::CSV; use Data::Dumper; die "ERROR: You have to provide at least one csv file." unless(@ARGV); my %RESULT; foreach my $filename (@ARGV) { if(grep { $_ eq $filename } keys %RESULT) { warn "WARN: Why dou you want to compare a file '$filename' whi +ch you have read already. Skipped."; next; } my $csv = Text::CSV->new( { binary => 1 } ) or die "Cannot use CSV: " . Text::CSV->error_diag (); open my $fh, "<", $filename or die "ERROR: Can't open '$filename': + $!"; my $counter = 0; while (my $row = $csv->getline($fh)) { $counter++; next if($counter < 2); unless(@$row <= 11) { die "ERROR: Line '$counter' of file '$filename' has wrong +format. Check your data crap."; } my $index = $row->[0]; my $index_id = $row->[1]; $RESULT{$filename}->{$index}->{$index_id} = { 'record' => $row + }; } $csv->eof or die "ERROR: Couldn't read csv file '$filename': " . $ +csv->error_diag(); close $fh; } # Missing keys my %FOUND_KEYS; my $files_loaded = scalar keys %RESULT; foreach my $filename (keys %RESULT) { foreach my $index (keys %{$RESULT{$filename}}) { foreach my $index_id (keys %{$RESULT{$filename}->{$index}}) { my $key = $index . '@' . $index_id; my $hash_ref = $FOUND_KEYS{$key} || {}; $hash_ref->{$filename} = 1; $FOUND_KEYS{$key} = $hash_ref; } } } foreach my $key (keys %FOUND_KEYS) { if(scalar keys %{$FOUND_KEYS{$key}} != $files_loaded) { print "INFO: Key combination '$key' only found in the followin +g files: " . join(', ', sort keys %{$FOUND_KEYS{$key}}) . "\n"; } }

Best regards

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2016-10-25 10:22 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (316 votes). Check out past polls.