Note that just file index information (from tell) could be stored as the hash value rather than the remainder of the line and the lines reread as required.
use warnings; use strict; my $Fn1 = shift; my $Fn2 = shift; die "Usage:\n" . "hash4Ans <file1> <file2>\n\n" . "file1 and file2 are the files to compare\n\n" if ! defined $Fn1 or ! defined $Fn2; my %File1; my %File2; open (inFile, "$Fn1") or die "Unable to open $Fn1 $!\n"; while (<inFile>) { chomp; my ($Key, $Tail) = /^((?:\w+\s){4})(.*)/g; $File1{$Key} = "$.:$Tail"; } close inFile; open (inFile, "$Fn2") or die "Unable to open $Fn2 $!\n"; while (<inFile>) { chomp; my ($Key, $Tail) = /^((?:\w+\s){4})(.*)/g; $File2{$Key} = "$.:$Tail"; } close inFile; my $Key; foreach $Key (%File1) { next if ! defined $File2 {$Key}; my ($Ln1, $Tail1) = $File1 {$Key} =~ /(\d)+:(.*)/g; my ($Ln2, $Tail2) = $File2 {$Key} =~ /(\d)+:(.*)/g; print "Match in line $Ln1 from $Fn1 with line $Ln2 from $Fn2:\n"; print "$Key$Tail1\n"; print "$Key$Tail2\n"; my ($Words1) = $Tail1 =~ /(?:\S+\s+){3}(.*)/g; my ($Words2) = $Tail2 =~ /(?:\S+\s+){3}(.*)/g; print "Note: Tailing word mismatch in previous pair.\n" if $Words1 n +e $Words2; print "\n"; } print "Done.\n";
word word word two 2 1.2 -2.3 apple six seven eight nine word perl word five 5 1.2 -2.3 grape six seven eight nine word word word seven 7 1.2 -2.3 lemon six seven eight nine word word soil three 3 1.2 -2.3 orange six seven eight nine word word word four 4 1.2 -2.3 onion six seven eight nine word word word one 1 1.2 -2.3 five six seven eight nine word word word six 6 1.2 -2.3 fig six seven eight nine word word word eight 8 1.2 -2.3 sausage six seven eight nine
oink word word one 1 2 -2.3 five six seven eight nine word oink word two 2 2 -2.3 apple six seven eight nine word word word three 3 2 -2.3 orange six seven eight spoon word oink word four 4 2 -2.3 onion six seven eight nine word word word five 5 2 -2.3 grape six seven eight plug word word word six 6 2 -2.3 fig six seven eight nine word word word seven 7 2 -2.3 sour six seven eight nine word oink word eight 8 2 -2.3 lemon six seven eight nine
Match in line 7 from data1.txt with line 6 from data2.txt: word word word six 6 1.2 -2.3 fig six seven eight nine word word word six 6 2 -2.3 fig six seven eight nine Match in line 3 from data1.txt with line 7 from data2.txt: word word word seven 7 1.2 -2.3 lemon six seven eight nine word word word seven 7 2 -2.3 sour six seven eight nine Note: Tailing word mismatch in previous pair. Done.
In reply to Re: 2 Hash Tables, 4 Keys...what to do?
by GrandFather
in thread 2 Hash Tables, 4 Keys...what to do?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |