http://qs1969.pair.com?node_id=11138651


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

There's More Than One Way To Do It: If you need to do a lot of lookups, then grep will quickly become inefficient, and instead it will likely be worth it to build a reverse lookup table (assuming it fits comfortably into memory). With the following, you can simply write $colors{green}, $colors{red}, etc.

use warnings; use strict; my %fruit = ( apple => ['red','green'], kiwi => 'green', banana => 'yellow', ); my %colors; for my $f (keys %fruit) { for my $c ( ref $fruit{$f} ? @{ $fruit{$f} } : $fruit{$f} ) { push @{ $colors{$c} }, $f; } } use Data::Dump; dd \%colors;

By the way, always Use strict and warnings!