in reply to Find all keys from a value in a hash

one of the rare cases where smartmatch is really helpful.

Note the warning, so not meant for production code.

use strict; use warnings; my %fruit = ( 'apple' => ['red','green'], 'kiwi' => 'green', 'banana' => 'yellow', ); for my $search ("green","red" ){ my @matches = grep { $search ~~ $fruit{$_} } keys %fruit; print "\n\n Matches for <$search> = @matches\n\n"; }

C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/smartmatch.pl Smartmatch is experimental at d:/tmp/pm/smartmatch.pl line 12. Matches for <green> = kiwi apple Matches for <red> = apple Compilation finished at Tue Nov 9 22:04:30

UPDATE

allowed multiple matches

update

seriously, your data structure is misdesigned . If you want singular elements put them into an array too.

my %fruit = ( 'apple' => ['red','green'], 'kiwi' => ['green'], 'banana' => ['yellow'], );

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery