Perhaps the following will assist you:

use Modern::Perl; use File::Slurp qw/read_file/; my ( $hash1, $hash2 ) = @ARGV or die $!; my %hash1 = map { /(.+?),(.+)/; $1 => $2 } grep /.,./, read_file $hash +1; my %hash2 = map { /(.+?),(.+)/; $1 => $2 } grep /.,./, read_file $hash +2; for my $key2 ( sort keys %hash2 ) { for my $key1 ( sort keys %hash1 ) { say "$key2\t$hash2{$key2}\t$key1" if $hash1{$key1} =~ /\b$hash +2{$key2}\b/; } }

Output:

k_b1 val_a1 k_a1 k_b2 val_a2 k_a1 k_b3 val_a3 k_a1 k_b4 val_a4 k_a2 k_b5 val_a5 k_a2

A regex is used within map to initialize the hashes with key/value pairs. The grep /.,./ helps insure that the regex can match--in case there are any blank lines (or other non-matching lines) in the files. Instead of splitting %hash1's values on commas, a word-boundary match is used to see if a value in %hash2 exists as a value in %hash1, and both keys are printed if so.

Hope this helps!

Update: Output modified based upon OP's reply to tobyink.


In reply to Re: how to loop through hash tables more efficiently by Kenosis
in thread updated_again: how to loop through hash tables more efficiently by lrl1997

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.