Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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.


Comment on Re: hash with both values and keys unique to each other
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-28 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls