FAQ: How do I look up a hash element by value? 
Author Message
 FAQ: How do I look up a hash element by value?

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.

+
  How do I look up a hash element by value?

    Create a reverse hash:

        %by_value = reverse %by_key;
        $key = $by_value{$value};

    That's not particularly efficient. It would be more space-efficient to
    use:

        while (($key, $value) = each %by_key) {
            $by_value{$value} = $key;
        }

    If your hash could have repeated values, the methods above will only
    find one of the associated keys. This may or may not worry you. If it
    does worry you, you can always reverse the hash into a hash of arrays
    instead:

         while (($key, $value) = each %by_key) {

         }

-

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to


or to the many thousands of other useful Usenet news groups.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-1999 Tom Christiansen and Nathan
    Torkington.  All rights reserved.

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

                                                           04.55
--
    This space intentionally left blank



Thu, 12 Aug 2004 14:17:01 GMT  
 FAQ: How do I look up a hash element by value?

(snipped)

Quote:
>   How do I look up a hash element by value?

* laughs *

This should read:

  "How do I look up a hash key by value?"

Quote:
>     If your hash could have repeated values, the methods above will only
>     find one of the associated keys. This may or may not worry you. If it
>     does worry you, you can always reverse the hash into a hash of arrays
>     instead:
>          while (($key, $value) = each %by_key) {

>          }

pffffttt... typical Perl 5 Cargo Cult Code.

My method is very simple and very efficient.

Godzilla!
--

#!perl

print "Content-type: text/plain\n\n";

print "Pure Numbers:\n\n";

$find_this = 2;

$Hash1{one} = 1;
$Hash1{two} = 2;
$Hash1{three} = 3;
$Hash1{four} = 1;
$Hash1{five} = 2;
$Hash1{six} = 3;

while ( ($key, $value) = each %Hash1 )
 {
  if ($value == $find_this)
   { print "Hash 1 Key $key Has A Value Of $find_this\n"; }
 }

print "\n\n";

print "Pure Alpha Characters:\n\n";

$find_this = def;

$Hash2{one} = abc;
$Hash2{two} = def;
$Hash2{three} = ghi;
$Hash2{four} = abc;
$Hash2{five} = def;
$Hash2{six} = ghi;

while ( ($key, $value) = each %Hash2 )
 {
  if ($value eq $find_this)
   { print "Hash 2 Key $key Has A Value Of $find_this\n"; }
 }

print "\n\n";

print "Pure Sub-strings:\n\n";

$find_this = Rocks;

$Hash3{one} = "Godzilla Rocks And Rolls!";
$Hash3{two} = "Perl 5 Cargo Cultists Do Not!";
$Hash3{three} = "Godzilla Rocks And Rolls!";
$Hash3{four} = "Perl 5 Cargo Cultists Do Not!";
$Hash3{five} = "Godzilla Rocks And Rolls!";
$Hash3{six} = "Perl 5 Cargo Cultists Do Not!";

while ( ($key, $value) = each %Hash3 )
 {
  if (index (" $value ", $find_this) > -1)
   { print "Hash 3 Key $key Contains A Value Of $find_this\n"; }
 }

PRINTED RESULTS:
________________

Pure Numbers:

Hash 1 Key five Has A Value Of 2
Hash 1 Key two Has A Value Of 2

Pure Alpha Characters:

Hash 2 Key five Has A Value Of def
Hash 2 Key two Has A Value Of def

Pure Sub-strings:

Hash 3 Key one Contains A Value Of Rocks
Hash 3 Key five Contains A Value Of Rocks
Hash 3 Key three Contains A Value Of Rocks



Fri, 13 Aug 2004 04:11:32 GMT  
 FAQ: How do I look up a hash element by value?
On Sun, 24 Feb 2002 19:11:32 -0800,

Quote:

> (snipped)

>>   How do I look up a hash element by value?

> * laughs *

> This should read:

>   "How do I look up a hash key by value?"

That depends entirely on what is meant by 'hash element'.

I actually suspect that the question should have read

    "How do I look up hash elements by value?"

Quote:
>>     If your hash could have repeated values, the methods above will only
>>     find one of the associated keys. This may or may not worry you. If it
>>     does worry you, you can always reverse the hash into a hash of arrays
>>     instead:

>>          while (($key, $value) = each %by_key) {

>>          }

> pffffttt... typical Perl 5 Cargo Cult Code.

Nonsense.

Do not pay any attention to what Godzilla says. It is a troll, and
has no decent working knowledge of Perl or programming in general.
Search groups.google.com to see a history of its posts and replies
to these posts.

Quote:
> My method is very simple and very efficient.

and none of the three 'methods' you suggest are really different from
the answer in the FAQ, except that your methods do not allow the
lookup of more than one value (except by reiterating over the hash),
and you don't store the result of the lookups anywhere so the rest of
the program can actually get at them.

Quote:
> while ( ($key, $value) = each %Hash2 )
>  {
>   if ($value eq $find_this)
>    { print "Hash 2 Key $key Has A Value Of $find_this\n"; }
>  }

I suspect that whoever wrote this FAQ entry figured that anyone who
wanted to limit the reverse lookup hash to a small subset of all
values would be clever enough to add a test around the push statement.
The fact that you believe that that is a revolutionary thing to do
just goes to show what the standard of your Perl knowledge is.

In short: Your "methods" cannot be used for anything but a small set
of special cases of the questions that the FAQ is trying to answer
here.

Martien
--
                                |
Martien Verbruggen              | That's funny, that plane's dustin'
Trading Post Australia Pty Ltd  | crops where there ain't no crops.
                                |



Fri, 13 Aug 2004 05:47:41 GMT  
 FAQ: How do I look up a hash element by value?

 (snipped)

Quote:
> >>   How do I look up a hash element by value?
> > * laughs *
> > This should read:
> >   "How do I look up a hash key by value?"
> That depends entirely on what is meant by 'hash element'.
> I actually suspect that the question should have read

Oh no! No! No!

The question should read,

 "Why did God, in Her infinite wisdom, give you the
  smarts of a common road toad?"

Godzilla!



Fri, 13 Aug 2004 07:34:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. hash of values and array of hashes of values and array of hashes

2. accessing elements of arrays as values in a hash

3. FAQ 4.58 How do I look up a hash element by value?

4. FAQ 4.58 How do I look up a hash element by value?

5. FAQ 4.58 How do I look up a hash element by value?

6. FAQ 4.58 How do I look up a hash element by value?

7. FAQ 4.58 How do I look up a hash element by value?

8. FAQ 4.58 How do I look up a hash element by value?

9. FAQ 4.58 How do I look up a hash element by value?

10. FAQ 4.58 How do I look up a hash element by value?

11. FAQ: How do I look up a hash element by value?

12. FAQ: How do I look up a hash element by value?

 

 
Powered by phpBB® Forum Software