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 |