The main thing here is that there is only one key: the first four words.

A few simplifying asumptions made here:
  • duplicate lines don't exist or don't matter
  • machine generated output so white space matches
  • case is important
  • Appart from the first, these are easy to fix (exercise for the reader?).

    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.

    Raise your eyes and look out of the rut.

    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

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.