You might double check your data because a) $fruit{kiwi} is "green" not "red", and b) matching an arrayref as is in $fruit{apple} against a string value using eq isn't going to do anything useful (it will be checking that the stringified representation of the array reference is your target, which it's most likely not).

Addendum: I'm guessing you were actually searching for "green" but you mucked up your data, in which case you would have found the one value in $fruit{kiwi}. Your problem then is that if your value is an array ref you'd need to again grep the values therein. WHile you can handle this with something like below:

my( @found ) = grep { ( ref $fruit{ $_ } eq 'ARRAY' ? (grep { $_ eq $s +earch } @{ $fruit{ $_ } } ) : $fruit{$_} eq $search ) } keys %fruit;

Cleaner (but slightly less memory efficient) would be to always store an arrayref even for single items; then the code would collapse to just:

my( @found ) = grep { grep $_ eq $search, @{ $fruit{ $_ } } } keys %fr +uit;

Edit: Also another problem since you're catching the result of the grep with my( $found ) you're only going to see the first value returned from grep (any further matches will be silently discarded because you've only got the one scalar variable named in the list-context my declaration); you need to use an array instead if you want the list of all the matches found.

Another thought:, If your goal is to have something where you can check "What fruits can be color x?" then using a hash of hashes (HoH) can reduce this to a simple grep { exists $fruits{ $_ }->{$color} } keys %fruits. You'd need to set up %fruits a bit differently.

my %fruits = ( apple => { red => 1, green => 1 }, kiwi => { green => 1 }, banana => { yellow => 1 } );

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Find all keys from a value in a hash by Fletch
in thread Find all keys from a value in a hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.