in reply to Re: Find all keys from a value in a hash
in thread Find all keys from a value in a hash

I came up with this:
#!/usr/bin/perl use strict; use warnings; my $search = $ARGV[0] || ''; if(!$search) { print "\n\n Need a color word to search! \n\n"; exit; } my %fruit = ( 'apple' => ['red','green'], 'kiwi' => ['blue'], 'banana' => ['yellow'], 'avocato' => ['brown'], ); my ($result) = grep { grep { $_ eq $search } @{$fruit{$_}} } keys %fru +it; print "\n\n Result:::: --> $result \n\n";

Replies are listed 'Best First'.
Re^3: Find all keys from a value in a hash
by Marshall (Canon) on Nov 11, 2021 at 21:35 UTC
    One thing that I like about this approach is to use anon ref's to array for all of the values if any key can be associated with more than a single color. This eliminates the checking of "if ref".