There is a problem with the common way we are making a "lookup by-value hash" to find the corresponding keys in the source hash:

What if two keys in a hash have the same value? When reversing the hash, or creating the lookup hash, the last identical value will clobber those before it. Here's some code to show you what I mean:

#!/usr/bin/perl -w use strict; use Data::Dumper qw(Dumper); my %hash = ( a => 1, b => 1, c => 1, d => 2, e => 2, f => 2, ); my %by_value = reverse %hash; print Dumper(\%by_value);

will print out something like:

$VAR1 = { '1' => 'a', '2' => 'e' };

This isn't what we want, there was a loss of information, during the copy to %by_value, the keys "a" and "e" were the last to have the values of 1, and 2 respectively. Since they were being assigned to a new hash, the last to get copied wins. Even worse, the order that the reverse is done in is not garaunteed to be the same, so you could get unpredictible results on other computers or possibly even from different versions of perl.

IMHO, a better way to do it would be to create a data structure that allows the lookups by value and preserves the original matching keys. Here is one possible way to do it:

my %hash = ( a => 1, b => 1, c => 1, d => 2, e => 2, f => 2, ); my %by_value; while(my($key, $value) = each %hash) { push @{ $by_value{$value} }, $key; } print Dumper(\%by_value);

which will print:

$VAR1 = { '1' => [ 'a', 'b', 'c' ], '2' => [ 'e', 'f', 'd' ] };

This above data structure correctly represents the relationship between a key and a value in a hash. That is, it allows any given value to have one or more keys in a hash. Accessing this structure is simple, if you want to see all the matching keys that have a value of "1", you can access the %by_value hash, like this:

print join "\n", @{ $by_value{1} };

In reply to (dkubb) Re: (3) Looking up a hash by value by dkubb
in thread Looking up a hash by value by tanger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.