Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: hash with both values and keys unique to each other

by sundialsvc4 (Abbot)
on Jul 12, 2012 at 20:47 UTC ( #981491=note: print w/replies, xml ) Need Help??

in reply to hash with both values and keys unique to each other

If you need to ensure that both hash-keys and hash-values are unique, then you probably need to use two hashes.   The second hash should contain an entry for every value that’s been used in the first one:   the associated value (in the second hash) doesn’t matter, as we only care whether-or-not the key (in the second hash) exists.

if ( (!exists($$hash1{$key} && (!exists($$hash2{$value}) ) { $$hash1{$key} = $value; $$hash2{$value} = 1; // DON'T CARE } else ... // (KEY AND/OR VALUE IS NOT UNIQUE...)

Note that $$hash{$key} is equivalent to $hash->{$key} ... take your pick.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://981491]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2017-12-13 04:50 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (345 votes). Check out past polls.