Anonymous Monk,
If I understand your requirements:
- Take action if a inner key exists as an inner key in another hash.
- Know which value came from which hash/key
- Take default action if no match
The clearest way I can think to do this is to walk through the all the keys of the second hash for each inner key in the first hash. The only optimization I have done is return once a match is found.
#!/usr/bin/perl
use strict;
use warnings;
my %hash1 = (
foo => { 'a' => 1, 'b' => 2, 'c' => 3 },
bar => { 'd' => 1, 'e' => 2, 'f' => 3 },
);
my %hash2 = (
blah => { 'm' => 1, 'd' => 2, 'a' => 3 },
asdf => { 'z' => 1, 'l' => 2, 'b' => 3 },
);
for my $outer_key ( keys %hash1 ) {
for my $inner_key ( keys %{ $hash1{$outer_key} } ) {
my ($h2_outer, $h2_inner) = Find_Key( $inner_key , \%hash2 );
if ( $h2_inner ) {
# do something : $outer_key, $inner_key, $h2_outer, and $h
+2_inner
}
else {
# do your other thing
}
}
}
sub Find_Key {
my ($match, $hash) = @_;
for my $key ( keys %$hash ) {
for ( keys %{ $hash->{$key} } ) {
return ($key, $_) if $_ eq $match;
}
}
return (undef, undef);
}
If this code is not any clearer, please let me know and I will give a blow by blow explanation.
Cheers - L~R
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.