in reply to Re^2: Need help with removing duplicate hash keys from array of hashes
in thread Need help with removing duplicate hash keys from array of hashes

That doesn't look like code. But your problem intrigued me. The below code gets you the duplicates removed similar to what you're looking for, but you'll need to improve it a bit* to get the exact data format you're after.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data = [ { 'prod_order' => '702164', 'operation' => '10', 'machine' => 'W01', }, { 'prod_order' => '702164', 'operation' => '10', 'machine' => 'W02', }, { 'prod_order' => '702164', 'operation' => '10', 'machine' => 'W01', }, { 'prod_order' => '702164', 'operation' => '10', 'machine' => 'W02', }, { 'prod_order' => '702164', 'operation' => '10', 'machine' => 'W03', }, { 'prod_order' => '702164', 'operation' => '100', 'machine' => 'W03', } ]; my %hash = (); my @newdata = grep { ! $hash{ $_->{operation} . $_->{machine} }++ } @$ +data; print Dumper @newdata;

Output:

$VAR1 = { 'operation' => '10', 'prod_order' => '702164', 'machine' => 'W01' }; $VAR2 = { 'operation' => '10', 'prod_order' => '702164', 'machine' => 'W02' }; $VAR3 = { 'operation' => '10', 'prod_order' => '702164', 'machine' => 'W03' }; $VAR4 = { 'operation' => '100', 'prod_order' => '702164', 'machine' => 'W03' };

* FYI, since the OP didn't show us any effort, I figure if they really need the result as $data = [ { .. => .. }, { .. => .. } ]; they can add a couple of characters and change a few sigils in what I've posted.



--chargrill
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Replies are listed 'Best First'.
Re^4: Need help with removing duplicate hash keys from array of hashes
by Anonymous Monk on Sep 25, 2006 at 13:59 UTC
    Thanks a lot for the solution