in reply to removing duplicates from an array of hashes
Note: Ordering in the array might changemy %seen; for my $i (reverse (0 .. @$ref-1)) { # replaces current item in array with last item # and removes the last element if id already seen $ref->[$i] = pop @$ref if $seen{$ref->[$i]->{id}}++; }
Update: inplace version that keeps the ordering
my %seen; my $removed = 0; for my $i (0 .. @$ref-1) { my $item = $ref->[$i]; $seen{$item->{id}}++ ? $removed++ : ($ref->[$i-$removed] = $item); } splice @$ref,-$removed;
Greetings,
Janek Schleicher
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: removing duplicates from an array of hashes
by AnomalousMonk (Archbishop) on Apr 17, 2014 at 17:32 UTC | |
by bigj (Monk) on Apr 18, 2014 at 14:23 UTC |