Re: Comparing items from multiple files

Yitzchak Scott-Thoennes <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Organization bs"d
Message-ID <[email protected]>
On Wed, Nov 19, 2003 at 10:02:34PM -0000, Peter Scott <[email protected]> wrote:
> Problem: multiple files containing essentially identical
> reports which I want to compare.  'diff' isn't the answer;
> in this case each file contains a bunch of stuff I'm not
> interested in that can change; I just want to get an element
> of each one and say which of the other files don't contain that
> element.

I would do it more like this:

use warnings;
use strict;

sub check_re { eval { qr/${\shift}/ } || return }

my $regex = "(".shift().")";
check_re($regex) or die "invalid regex: $regex\n";

my %files;
my %data;

while (<>) {
   ++$files{$ARGV};
   next unless /$regex/;
   ++$data{$+}{$ARGV};
}

my @files = sort keys %files;

for my $value (sort keys %data) {
   if (keys %{$data{$value}} != @files) {
      printf "In %s but not in %s: %s\n",
          join(", ",keys %{$data{$value}}),
          join(", ",grep !exists $data{$value}{$_}, @files),
         $value;
   }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.