davidfilmer has asked for the wisdom of the Perl Monks concerning the following question:
I want to find out Alice's last name. Of course, I could just slog through the hash:my %uid =( 1 => { 'firstname' => 'Alice', 'lastname' => 'Foo', }, 2 => { 'firstname' => 'Bob', 'lastname' => 'Bar', }, );
But that seems inelegant. Is there a more elegant way?foreach my $key( keys %uid ){ if( $uid{$key}{'firstname'} eq 'Alice' ){ print "lastname is $uid{$key}{'lastname'}\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: In search of elegant code - looking deeply into a hash
by NetWallah (Canon) on Mar 02, 2016 at 03:49 UTC | |
|
Re: In search of elegant code - looking deeply into a hash
by beech (Parson) on Mar 02, 2016 at 03:13 UTC | |
|
Re: In search of elegant code - looking deeply into a hash
by Laurent_R (Canon) on Mar 02, 2016 at 07:26 UTC | |
|
Re: In search of elegant code - looking deeply into a hash
by flowdy (Scribe) on Mar 02, 2016 at 12:18 UTC | |
|
Re: In search of elegant code - looking deeply into a hash
by Anonymous Monk on Mar 02, 2016 at 13:25 UTC |