Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Remove duplicate key value pair in hash

by reaper9187 (Scribe)
on Sep 19, 2014 at 19:05 UTC ( [id://1101275]=perlquestion: print w/replies, xml ) Need Help??

reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I need some help eliminating duplicate key value pairs in a hash. The most simple reproducible example is as follows:
my %hash = ('one' => '1', 'two' => '2','1'=>'one');
As you can see, I have two entries with same key-value pairs. Is it possible to remove all such possible combinations ??

Update

Hello monks, I need some help eliminating duplicate key value pairs in a hash. The most simple reproducible example is as follows:
my %hash = ('aaa' => 'bbb', 'bbb' => 'aaa','ccc'=>'bbb','bbc'=>'ccc',' +abc'=>'123');
As you can see, I have two entries with same key-value pairs. Is it possible to remove all such possible combinations ??

Replies are listed 'Best First'.
Re: Remove duplicate key value pair in hash
by Your Mother (Archbishop) on Sep 19, 2014 at 19:14 UTC

    Kind of an odd question, they are not the same key value pairs; unless you mean numeral versus spelled number(?). They are flipped key value pairs. You have to make a choice which is the side to remove or call a duplicate. This may or may not do what you expect–

    use Data::Dump "pp"; my %hash = ('one' => '1', 'two' => '2','1'=>'one'); my %seen; for my $key ( keys %hash ) { delete $hash{$key} if $seen{$key} || $seen{$hash{$key}}; $seen{$key}++; $seen{$hash{$key}}++; } pp \%hash; -- { 1 => "one", two => 2 }
Re: Remove duplicate key value pair in hash
by CountZero (Bishop) on Sep 19, 2014 at 19:54 UTC
    By definition keys cannot be duplicate. Keys are always unique (or they are not keys).

    If you happen to have two records with the same key, then one will always have to overwrite the other.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Remove duplicate key value pair in hash
by Anonymous Monk on Sep 19, 2014 at 19:15 UTC

    'one' and '1' are completely different and not the same.

    Keys and Values are quite distinct ideas as well.

    What exactly are you trying to accomplish here, and what's the context? More realistic data would probably help a lot.

Re: Remove duplicate key value pair in hash
by Anonymous Monk on Sep 19, 2014 at 19:37 UTC

    Yes, it's possible, but it would help if you could specify some sample output?

    BTW, it'd be best if you marked your update in the node that you updated.

      My sample output would be :
      my %hash = ('aaa'=>'bbb','ccc'=>'bbb','abc'=>'123');
      Sorry about that. Will keep that in mind the next time
        That no longer makes sense since you edited your original post, again. Make up your mind and post a complete problem description as per this, and mark your updates, you've already been reminded once (see also here).
Re: Remove duplicate key value pair in hash
by reaper9187 (Scribe) on Sep 19, 2014 at 19:21 UTC
    Thank you very much for your quick responses. I know that these are distinct pairs. But for the case I'm considering, these are just plain strings and can be considered the same (duplicates) for all intents and purposes. Does it help ???

    **UPDATE : I have now updated the example to illustrate one of the probs for the soln presented by YourMother. Is there a way to overcome this ??
      No, since you still haven't specified whether you mean that you want pairs such as (foo=>'bar',bar=>'foo') removed, or whether you consider "one" and "1" to be equal.
        I have modified the example now to remove the confusion. I hope it is clear now.
      Don't update and change the example, now the previous answers look incomprehensible. When you edit a post (especially a post that have received some answer), please don't change the content without explicitly saying what you changed or added.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1101275]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found