NOTE: The following assumes both keys and value have to match. If you just want to check for value matches, there are a couple different ways you could go about it, depending on whether all values in each hash are unique or not, and if not, whether you want all matches listed or just the first.
use strict; use warnings; my ($k1, $k2, %foo, %bar); %foo = ( 'a' => { 'x' => 1, 'y' => 3 }, 'b' => { 'x' => 4, 'y' => 4 } ); %bar = ( 'a' => { 'x' => 1, 'y' => 2 }, 'b' => { 'x' => 3, 'y' => 4 } ); for $k1 (sort keys %foo) { for $k2 (sort keys %{$foo{$k1}}) { print "\$foo{$k1}{$k2} eq \$bar{$k1}{$k2} with value $foo{$k1} +{$k2}\n" if $foo{$k1}{$k2} eq $bar{$k1}{$k2}; } }
EDIT: Here's some code to report all value matches:
use strict; use warnings; use Data::Dumper; my ($k1, $k2, %foo, %bar, %fval, %bval); %foo = ( 'a' => { 'x' => 1, 'y' => 3 }, 'b' => { 'x' => 4, 'y' => 4 } ); %bar = ( 'a' => { 'x' => 1, 'y' => 2 }, 'b' => { 'x' => 3, 'y' => 4 } ); for $k1 (keys %foo) { for $k2 (keys %{$foo{$k1}}) { push @{$fval{$foo{$k1}{$k2}}}, "$k1\t$k2"; } } for $k1 (keys %bar) { for $k2 (keys %{$bar{$k1}}) { push @{$bval{$bar{$k1}{$k2}}}, "$k1\t$k2"; } } for $k1 (sort keys %fval) { if (exists $bval{$k1}) { print "Value $k1 matches for:\n"; for (sort @{$fval{$k1}}) { @_ = split /\t/; print " \$foo{$_[0]}{$_[1]}\n"; } for (sort @{$bval{$k1}}) { @_ = split /\t/; print " \$bar{$_[0]}{$_[1]}\n"; } print "\n"; } }

In reply to Re: what's the best way to compare two 2D hashes? by TedPride
in thread what's the best way to compare two 2D hashes? by coontie

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.