Well you run into problems there, because the value may not be unique.
%vegetable_costs = ( Beans => 300,
Carrots => 200,
Apple => 250,
Asparagus => 300
);
Now it depends on the order of the hash which one you will hit first,
and order can't be relied on.
If you can guarantee unique values then one verbose way:
my $test_val = '300';
while (my ($key, $val) = each %vegetable_costs) {
if ($val eq $test_val) {
print "Vegetable with cost $val: $keyn";
last;
}
}
A non-verbose way is just to shift the values to be keys in a different
temporary hash and then use the same lookup. This has problems on large
hashes requiring lots of memory and for complex values as they can't be
used as keys (in most cases), but then the simple 'eq' operator above
isn't going to work on them either.
Basically you have entered the realm of demons...
http://danconia.org