in reply to Re: Most elegant way to dispose of duplicates using map
in thread Most elegant way to dispose of duplicates using map

Perfect, this is exactly what I was looking for. Thanks!
  • Comment on Re^2: Most elegant way to dispose of duplicates using map

Replies are listed 'Best First'.
Re^3: Most elegant way to dispose of duplicates using map
by rashley (Scribe) on Oct 30, 2006 at 21:18 UTC
    Just for the sake of intelectual growth, how could one tweak this to eliminate duplicates of id/versions pairs, instead of just id?
      You just need to make a key for the %seen hash that is your id and version joined together in some way. Here I join them with a colon

      use strict; use warnings; use Data::Dumper; my @partTuples = ( q{abc,1.1,apple}, # 1st element q{def,3.6,orange}, # no dups. so OK q{abc,1.5,pear}, # OK id only dup. q{abc,1.1,kiwi}, # dup. id and version q{ghi,1.2,peach}, # no dups. so OK q{xyz,1.1,plum}, # OK version only dup. ); my %seen = (); my @uniquePTs = grep {! $seen{join q{:}, $_->{id}, $_->{version}} ++} map { { id => $_->[0], version => $_->[1], classification => $_->[2] } } map { [split m{,}] } @partTuples; print Dumper(\@uniquePTs);

      The output is

      $VAR1 = [ { 'version' => '1.1', 'classification' => 'apple', 'id' => 'abc' }, { 'version' => '3.6', 'classification' => 'orange', 'id' => 'def' }, { 'version' => '1.5', 'classification' => 'pear', 'id' => 'abc' }, { 'version' => '1.2', 'classification' => 'peach', 'id' => 'ghi' }, { 'version' => '1.1', 'classification' => 'plum', 'id' => 'xyz' } ];

      Cheers,

      JohnGG

        I'd suggest using a very careful delimiter if you want to take a key and serialize it in this manner. if for instance, you used a comma, you have a key value pair of "1,2" and "3" and another of "1" and "2,3", they may evaluate the same.
        I really need to crack the magic map/grep code.

        I see what you're doing, and I think a colon will work for the data I'm dealing with, but if I understand this, I'll need to change the way I'm putting my original @partTuples together. This:

        @partTuples = map { my @t = split(','); {id=>$t[0], version=>$t[1], classification=>$t[2]} } @partTuples;
        Isn't working, since we're doing the mapping later on, but I'm not sure what you're code is expecting.

        Thanks for all the help.