Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Bi-Directional Hash Lookup

by thor (Priest)
on Oct 11, 2005 at 11:56 UTC ( [id://499128]=note: print w/replies, xml ) Need Help??


in reply to Bi-Directional Hash Lookup

What about something like this
#assume that %hash is already initialized %hash = (%hash, reverse %hash);
Of course, you are still storing everything twice, but in order to be able to lookup on either value, you'll need something like this.

thor

Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come

Replies are listed 'Best First'.
Re^2: Bi-Directional Hash Lookup
by blazar (Canon) on Oct 11, 2005 at 12:14 UTC
    I had thought of this too, but the OP claimed that each of the keys and the values are unique. He didn't exclude that any of the keys may also be a value, although it is definitely reasonable to doubt about this. I'm pointing this out just to let him/her know that this solution may fail in the case it is so:
    use Data::Dumper; my %hash=(foo => 'bar', bar => 'baz'); %hash=(%hash, reverse %hash); print Dumper \%hash;

    Incidentally I wouldn't have used your smart and elegant syntax -of which I may have thought, but but which didn't occur to me- and I would have done a clumsier

    @hash{values %hash} = keys %hash;
    instead.
      He didn't exclude that any of the keys may also be a value, although it is definitely reasonable to doubt about this.
      I took the OP to mean that each value would be unique in the hash. That is to say that for any key or value in the hash, there would not exist any other key or value that would have the same..erm...value.

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://499128]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found