Although not familiar with $cgi->param(), from your OP it looked like it returned a list of strings that you assigned to an array, each string being three comma-delimited fields. I just made up some gash data that had the same structure. The code I gave goes from the array of strings though to the array of unique part tuples hashes without stopping along the way. You could even take it further by feeding the return of $cgi->param() straight into the maps, like this.

my @uniquePTs = grep {! $seen{join q{:}, $_->{id}, $_->{version}} ++} map { { id => $_->[0], version => $_->[1], classification => $_->[2] } } map { [split m{,}] } $cgi->param('partID');

Reading this code from the bottom up you

1) call $cgi->param() which returns a list of strings that are passed, one at a time, into the bottom map

2) things are passed into and out of map and grep in $_ so the bottom map takes the string passed in and splits it on commas. The resultant list is placed inside anonymous array constructors [ ... ] so a reference to the new anonymous array is passed out to the map above, again in $_

3) in the second map the value passed in in $_ is a reference to an array so to use it we need to dereference it like $_->[0] etc. In this map we construct an anonymous hash using { ... } and populate the key/value pairs. The reference to the hash is in turn passed out to the grep

4) in the grep we again need to dereference $_, this time to access the hash like $_->{id}. By combining the values for the "id" and "version" keys we can construct a key for the %seen hash that we use to detect duplicates. We grep out only those anonymous hashes who's "id" and "version" haven't already occurred in the %seen hash.

5) finally, those hash references that have passed the grep are assigned to the @uniquePTs array as the grep{...} map{...} map{...} list returns a list.

I hope I've explained this adequately but I'm rushing a bit as I have to leave for an appointment soon. If I've totally misunderstood what $cgi->param('partID'); does, let me know and I'll adjust the code.

Cheers,

JohnGG


In reply to Re^6: 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.