Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Most elegant way to dispose of duplicates using map

by rashley (Scribe)
on Oct 30, 2006 at 16:07 UTC ( #581321=perlquestion: print w/replies, xml ) Need Help??

rashley has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!

I have this sub, that takes a cgi param and splits it up into a list of hashes:

sub extractPartTuplesFromURL { my $cgi = shift; my @partTuples = $cgi->param('partID'); @partTuples = map { my @t = split(','); {id=>$t[0], version=>$t[1], classification=>$t[2]} } @partTuples; return \@partTuples; }
What I need to do is get rid of the duplicates bassed on "id", or better yet, prevent them from getting in my list to begin with.

I'm sure I could slug through this, but everything I've come up with is very clunky, and I'm sure there is a more "monkish" way to do it.

Thoughts? Thanks!

Replies are listed 'Best First'.
Re: Most elegant way to dispose of duplicates using map
by davorg (Chancellor) on Oct 30, 2006 at 16:26 UTC
      Perfect, this is exactly what I was looking for. Thanks!
        Just for the sake of intelectual growth, how could one tweak this to eliminate duplicates of id/versions pairs, instead of just id?
Re: Most elegant way to dispose of duplicates using map
by borisz (Canon) on Oct 30, 2006 at 16:30 UTC
    Hope this is what you have in mind.
    sub extractPartTuplesFromURL { my $cgi = shift; my @partTuples = $cgi->param('partID'); my %dupes; @partTuples = map { my @t = split(','); $dupes{ $t[0] }++ ? () : +{ id => $t[0], version => $t[1], classification => $t[2] } } @partTuples; return \@partTuples; }
    Boris
Re: Most elegant way to dispose of duplicates using map
by davidrw (Prior) on Oct 30, 2006 at 18:03 UTC
Re: Most elegant way to dispose of duplicates using map
by Anonymous Monk on Oct 31, 2006 at 09:22 UTC
    I find the most elegant way to remove duplicates is to not use map:
    my %tmp; @tmp{@partTuples} = (); @partTuples = keys %tmp;
    Although I would use map if there was a need to keep the order.
Re: Most elegant way to dispose of duplicates using map
by Anonymous Monk on Oct 31, 2006 at 15:58 UTC
    Assuming that What I need to do is get rid of the duplicates bassed on "id" means that you should not have two items with the same id, and also assuming that the returned order doesn't matter, I'd use:
    sub extractPartTuplesFromURL { my $cgi = shift; my %partTuples; foreach my $tuple ($cgi->param('partID')) { my($id, $version, $classificiation) = split(/,/, $tuple); next if $partTuples{$id}; # Keeps the first entry. Remove it i +f you want to keep the last. $partTuples{$id} = [$version,$classificiation]; } map {{id => $_, version => $partTuples{$id}->[0], classification = +> $partTuples{$id}->[1]}} keys %partTuples; }
    Or just return \%partTuples and change the caller.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://581321]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2021-01-17 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?