#filiecopare compares two lists, outputs files that match both,in 1 missing in 2, and in 2 missing in 1 #4/4/04 (love that date!) #Charles Oppenheimer - distributed under perl artistic license. go crazy. $file1 = $ARGV[0] || "ourvalues.txt"; $file2 = $ARGV[1] || "theirvalues.txt"; ########### output files $matchesboth = "filematchesboth.txt"; $missingin2 = "in1missingin2.txt"; $missingin1 = "in2missingin1.txt"; print "Start up - comparing files $file1 and $file2\n"; print "opening files...\n"; open(MATCHESBOTH, "> $matchesboth") || die "Could not open file $matchesboth : $!"; open(MISSINGING2, "> $missingin2") || die "Could not open file $missingin2 : $!"; open(MISSINGING1, "> $missingin1") || die "Could not open file $missingin1 : $!"; print "creating hash1\n"; %filehash1 = &filetohash($file1); print "creating hash2\n"; %filehash2 = &filetohash($file2); ### main part, compare those in 1 that are in 2 and aren't in 2 #check for matches, and those in file 1 but not 2 $missing2; $matchboth; $missing1; print "checking for matches in both files and values in file 1 and missing in file2\n"; for my $key (keys %filehash1) { #print "checking key $key for filehash1\n"; if ($filehash2{$key} == 1) { #print "a hash2 of this value equals on i guess..."; print MATCHESBOTH "$key\n"; $matchboth++; } else { print MISSINGING2 "$key\n"; #print "didn't match hash2 = $key\n"; $missing2++; } } #do a second pass for those in file 2 but not 1 print "checking for values missing in 2 and missing in file1\n"; for my $key (keys %filehash2) { unless ($filehash1{$key} == 1) { print MISSINGING1 "$key\n"; $missing1++; } } print "found $matchboth records matching both files, $missing2 records in file 1 missing from file 2, and $missing1 records in file 2 missing from file 1\n"; sub filetohash { my $counter; my $dir = shift; my %filehash; open(FILE, "<$dir") || die $!; while () { #print "chomping file $dir, line = $line"; chomp $_; #remove newline s/^ *//; #remove leading spaces s/ *$//; #remove trailing spaces $filehash{$_} = 1; #set the currentline to 1, for comparison later $counter++; } print "counted $counter lines for file $dir\n"; return %filehash; }