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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |