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