in reply to Re^3: Most elegant way to dispose of duplicates using map
in thread Most elegant way to dispose of duplicates using map
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Most elegant way to dispose of duplicates using map
by exussum0 (Vicar) on Oct 31, 2006 at 14:43 UTC | |
by johngg (Canon) on Oct 31, 2006 at 14:53 UTC | |
|
Re^5: Most elegant way to dispose of duplicates using map
by rashley (Scribe) on Oct 31, 2006 at 15:00 UTC | |
by johngg (Canon) on Oct 31, 2006 at 15:43 UTC | |
by rashley (Scribe) on Oct 31, 2006 at 15:38 UTC |