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
allowed multiple matches
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
In reply to Re: Find all keys from a value in a hash
by LanX
in thread Find all keys from a value in a hash
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |