in reply to Extract hash keys for values stoted in array
Based on the little you've told us, you could do:
... my @selectedKeys = grep{ $hash{ $_ } eq $scalar } keys %hash; ...
But doing that, ie. looking up keys in a hash from the values, defeats the purpose of using a hash.
The chances are that you should really be building the hash the other way around.
Instead of
you should probably be doing something like$hash{ $key } = $value;
.push @{ $hash{ $value } }, $key;
Then, when you need the 'keys' associated with a particular 'value' you find them directly:
... my @selectKeys = @{ $hash{ $scalar } };
thus avoiding a potentially costly linear search.
If you were to describe the bigger picture of your code, and post the code you have so far, rather than just this tiny fragment of requirement, then you'd probably get not just a solution to the small problem, but good advice on solving the bigger problem more effectively.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Extract hash keys for values stoted in array
by onlyIDleft (Scribe) on May 01, 2011 at 22:20 UTC | |
by BrowserUk (Patriarch) on May 02, 2011 at 00:01 UTC | |
by onlyIDleft (Scribe) on May 02, 2011 at 19:56 UTC |