Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by Anonymous Monk
on Jul 12, 2012 at 17:57 UTC ( #981457=note: print w/ replies, xml ) Need Help??


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

Assuming your question is "How to keep the values of a hash unique?", the answer is, well, two hashes:

my (%hash, %rhash); while (<>) { chomp; my ($key, $val) = split(/\t/, $_, 2); next if (exists $hash{$key} or exists $rhash{$val}); $hash{$key} = $val; $rhash{$val} = 1; }

Of course, there will be some manual housekeeping of the second hash. You could make it look nicer by creating an object that mimics a hash.


Comment on Re: hash with both values and keys unique to each other
Download Code
Re^2: hash with both values and keys unique to each other
by perlkhan77 (Acolyte) on Jul 12, 2012 at 19:19 UTC

    This is how I was able to do it. But it might not be a generalized solution

    $seen{$chr1}++; $seen{$chr2}++; if (($seen{$chr1}>1)||($seen{$chr2}>1)){ next; } else{ $hash_chr{$chr1}=$chr2; }

      Since your structure is

      loop { if (condition) { next; } # no else! other code; }

      You don't actually need the else block. Removing it will make your code look quite a bit more clean.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-04-20 03:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls