Here's a straightforward approach:
If the number of keys in one hash is much greater than the number of keys in the other, it may pay to optimize this search so that the iteration happens over the keys of the hash with fewer keys.for my $k1 ( keys %foo ) { next if not defined $bar{ $k1 }; for my $k2 ( keys %{ $foo{ $k1 } } ) { next if not defined $bar{ $k1 }{ $k2 }; dance() if $foo{ $k1 }{ $k2 } eq $bar{ $k1 }{ $k2 }; } }
I show a slightly "fancier" approach below, because I think you may find it instructive, but IMO the first approach is far preferable for production code:
for my $k1 ( grep defined $h2{ $_ }, keys %h1 ) { for my $k2 ( grep defined $h2{ $k1 }{ $_ }, keys %{ $h1{ $k1 } } ) { dance() if $h1{ $k1 }{ $k2 } eq $h2{ $k1 }{ $k2 }; } }
the lowliest monk
In reply to Re: what's the best way to compare two 2D hashes?
by tlm
in thread what's the best way to compare two 2D hashes?
by coontie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |