Sometimes I need to compare two similar files and get to know what entry is in file1, what entry is in file2, I have tried read each file and loop through the files line by line and compare the lines with regex, but it turned out to be very dumb idea and I never got it working. After reading more Perl doc and mailling list, apparently, the perl hash variable is the way to define uniqness of a record, here is the perl script I I got it working:
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
tie my @array1, 'Tie::File', "/tmp/u1.txt" or die "Could not tie: $!";
tie my @array2, 'Tie::File', "/tmp/u2.txt" or die "Could not tie: $!";
my %index;
foreach my $entry (@array1) {
if (not exists $index{$entry}) {
$index{$entry} = 1;
}
}
foreach my $entry (@array2) {
if (not exists $index{$entry}) {
$index{$entry} = 2;
}
else {
$index{$entry} += 2;
}
}
foreach my $entry (keys %index) {
if ($index{$entry} == 1) {
print "Entry $entry is only in file one\n";
}
elsif ($index{$entry} == 2) {
print "Entry $entry is only in file two\n";
}
elsif ($index{$entry} == 3) {
print "Entry $entry is in both files\n";
}
else {
print "Entry $entry is screwed up!\n";
}
}
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
tie my @array1, 'Tie::File', "/tmp/u1.txt" or die "Could not tie: $!";
tie my @array2, 'Tie::File', "/tmp/u2.txt" or die "Could not tie: $!";
my %index;
foreach my $entry (@array1) {
if (not exists $index{$entry}) {
$index{$entry} = 1;
}
}
foreach my $entry (@array2) {
if (not exists $index{$entry}) {
$index{$entry} = 2;
}
else {
$index{$entry} += 2;
}
}
foreach my $entry (keys %index) {
if ($index{$entry} == 1) {
print "Entry $entry is only in file one\n";
}
elsif ($index{$entry} == 2) {
print "Entry $entry is only in file two\n";
}
elsif ($index{$entry} == 3) {
print "Entry $entry is in both files\n";
}
else {
print "Entry $entry is screwed up!\n";
}
}