in reply to get a key for a value revisited
use strict; use warnings; use Data::Dumper; # %data is an example Hash-of-Arrays structure # where the hash has 3 keys (a,b,c) and each # key has 3 values: my %data = ( a => [1..3], b => [4..6], c => [7..9], ); #print Dumper(\%data); print get_key(3), "\n"; print get_key(8), "\n"; print get_key(0), "\n"; sub get_key { my $value = shift; for my $key (keys %data) { for my $val (@{ $data{$key} }) { if ($val eq $value) { return $key; } } } return 'key not found'; } __END__ a c key not found
|
|---|