in reply to map and grep (but for hashes)

In general, I find using map or grep on keys %hash is usually all you need. If you really want to get complex, there are some ideas in the book 'Higher Order Perl' that allow you to do some interesting things.

But honestly, what's wrong with: grep { $hash{$_} == $expr } keys %hash; ?

Replies are listed 'Best First'.
Re^2: map and grep (but for hashes)
by zerohero (Monk) on Jan 30, 2009 at 20:03 UTC

    The result of this expression is an array, not a hash. So now we need to combine the values from the keys and produce a hash. The expression is starting to get long (esp. compared with how map and grep look for arrays).

    A lot of transformations want me to keep things as hashes. I'd describe most data structures in perl as being multi-level hashes, with a smattering of arrays, not vice versa.

      The expression above will return a list of keys. Then getting your filtered hash is as simple as:
      my %new_hash; @new_hash{@filtered_keys} = @old_hash{@filtered_keys};