Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2016-10-22 06:42 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (292 votes). Check out past polls.