in reply to Perl: How to print unmatched data after comparison of two files?

If you were forced to look at the files in the order file 1, file 2, you could store the file 1 data in an hash of arrays, with names as keys. Something like this:

#!/usr/bin/perl use strict; use warnings; my $fileNameA = $ARGV[0]; my $fileNameB = $ARGV[1]; my %outputHash = (); my @line; my $x = 0; open (my $inputA, "<", $fileNameA); while (<$inputA>) { next unless $_ =~ m/\w/; @line = split(" ", $_); push (@{$outputHash{$line[1]}}, $_); } open (my $inputB, "<", $fileNameB); while (<$inputB>) { next unless $_ =~ m/\w/; @line = split(" ", $_); if (exists ${$outputHash{$line[2]}}[0]) { for ($x = 0; $x < @{$outputHash{$line[2]}}; $x += 1) { print STDERR "${$outputHash{$line[2]}}[$x]"; } } }

But this would only work if the names in file 2 are unique. And it's more efficient to do it the opposite way around if possible, as suggested above.

-Michael
  • Comment on Re: Perl: How to print unmatched data after comparison of two files?
  • Download Code