in reply to Re: One to many hash question
in thread One to many hash question

Thanks, I was just hoping that there was some way to get information if exists returned more than one value without having to traverse the hash for every comparison....

Writing code is an artform

Replies are listed 'Best First'.
Re^3: One to many hash question
by GrandFather (Saint) on May 06, 2008 at 22:00 UTC

    You can't, unless you create a reverse lookup as dragonchild suggests. Consider:

    use strict; use warnings; use Data::Dump::Streamer; my %hash = ( doubleCheeseBurger => 'burger', cheeseBurger => 'burger', cheeseSandwich => 'sandwich', tomatoSandwich => 'sandwich', ); my %rHash; push @{$rHash{$hash{$_}}}, $_ for keys %hash; Dump (\%hash, \%rHash);

    Prints:

    $HASH1 = { cheeseBurger => 'burger', cheeseSandwich => 'sandwich', doubleCheeseBurger => 'burger', tomatoSandwich => 'sandwich' }; $HASH2 = { burger => [ 'cheeseBurger', 'doubleCheeseBurger' ], sandwich => [ 'cheeseSandwich', 'tomatoSandwich' ] };

    Note that the reverse lookup hash has a list of entries for each key because of the many to one mappings in the original hash.


    Perl is environmentally friendly - it saves trees