In file1, is word1..word4 unique (only 1 line has any specific word1..word4)? Is it feasible to read in the whole file? If so, my approach would be something like:
my %file1; while (<FILE1>) { # Capture first four words, and everything after the next 3 words +(numbers) my ($k1, $k2) = /(\w+(?:\s+\w+){3})(?:\s+\w+){3}(.*)/; # Index by k1, store k2 and line $file1{$k1} = [$k2, $_]; } while (<FILE2>) { my ($k1, $k2) = .... # same regex as before if ($file1{$k1}) { # print file1 line and file2 line print $file1{$k1}->[1]; print; # Check if k2's are equal if ($file1{$k1}->[0] ne $k2) { print "K2's are not equal! (see lines printed above)\n"; } } }
I've probably misunderstood some of what you want, but I think the parsing of the keys could be a significant help to you.

Update: fixed regex as per nobull's note.


Caution: Contents may have been coded under pressure.

In reply to Re: 2 Hash Tables, 4 Keys...what to do? by Roy Johnson
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.