My personal solution would be to reverse the hash so the
original values are keys, but the resultant values for each
key would be an array reference so that the lookup would
return all appropriate original keys.
Update: Some code
my %original_hash = ( first => 'alpha',
second => 'beta',
prime => 'alpha', );
foreach $key (keys %original_hash)
{
push @{$reverse_hash{$original_hash{$key}}}, $key;
}
foreach $rkey (keys %reverse_hash)
{
print "$rkey = ".(join ', ', @{$reverse_hash{$rkey}})."\n";
}
print "\n";
my %test_hash = reverse %original_hash;
foreach $tkey (keys %test_hash)
{
print "$tkey = $test_hash{$tkey}\n";
}
Just a little demonstration loop at the end.
Update' I managed to reproduce the solution in the
Cookbook without checking it first. First off, I'm happy
about doing such. Second, I figured I'd go ahead and
attribute it as such a solution, even though I didn't look
ahead of time.
ALL HAIL BRAK!!!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.