in reply to Finding hashes in hashes of hashes

First of all you should be using strict & warnings (I Think you are not) as you are not declaring your variables with 'my' or 'our'

Anyway, Something like t his might work for you.

my %families = ( flintstones => { father => "fred", mother => "willma", kid => "bambam", }, simpsons => { father => "homer", mother => "marge", kid => "bart", }, ); print getfamily(\%families,'bart','kid'); sub getfamily { my ($families,$member,$type) = @_; foreach my $family ( keys %{ $families } ) { while ( my ($status,$name) = each %{ $families{$family} } ) { if ( $status eq $type && $member eq $name ) { return $family; } } } return undef; }

Update: Modified code to account for status as per bobf replay.

I'll leave it to the OP to choose what he wants to do with duplicates.

Replies are listed 'Best First'.
Re^2: Finding hashes in hashes of hashes
by bobf (Monsignor) on Dec 28, 2009 at 01:36 UTC

    That's a step in the right direction, but it fails to test the relationship (called $status in your example) to ensure the match is on the 'kid' key. Your code, as written, will return the surname of the first family tested that contains the name of interest. For example, if the Flintstones' father was also named 'bart' and if that family came first in the foreach loop, the function would return 'flintstones' rather than 'simpsons'.

    BTW, the OP did not specify how the code should handle multiple matches (in the event more than one family has a 'kid' named 'bart').