Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: removing duplicates from an array of hashes

by atcroft (Abbot)
on Apr 17, 2014 at 04:02 UTC ( [id://1082586]=note: print w/replies, xml ) Need Help??


in reply to removing duplicates from an array of hashes

This was the first thing that popped into my head:

Code:

perl -MData::Dumper -le ' my $ref = []; $ref->[0] = { id => "a" }; $ref->[1] = { id => "b" }; $ref->[2] = { id => "c" }; $ref->[3] = { id => "b" }; print Data::Dumper->Dump( [ \$ref, ], [ qw( *ref ) ] ); my $temp; my %seen; while ( my $t = shift @{$ref} ) { if (not defined $seen{$t->{id}}) { push @{$temp}, $t; $seen{$t->{id}}++; } } print Data::Dumper->Dump( [ \$temp, ], [ qw( *temp ) ] ); '
Output:
$ref = \[ { 'id' => 'a' }, { 'id' => 'b' }, { 'id' => 'c' }, { 'id' => 'b' } ]; $temp = \[ { 'id' => 'a' }, { 'id' => 'b' }, { 'id' => 'c' } ];

Hope that helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1082586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found