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


In reply to Re^4: Most elegant way to dispose of duplicates using map by johngg
in thread Most elegant way to dispose of duplicates using map by rashley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.