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

Hi,

Using the data structure, I need to remove duplicates using 'operation' and 'machine' keyword.

$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', } ];

Need to remove duplicates using 'operation' and 'machine' keyword. The ouput should be stored as follows

$data = [ { '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', } ];

Can anyone give me sample code?

Code tags added by GrandFather

2006-09-25 Retitled by GrandFather, as per Monastery guidelines
Original title: 'Need help'

Replies are listed 'Best First'.
Re: Need help with removing duplicate hash keys from array of hashes
by jwkrahn (Monsignor) on Sep 25, 2006 at 05:18 UTC
    $ perl -le' 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", } ]; print Dumper $data; @$data = do { my %seen; grep { !$seen{ $_->{ operation }, $_->{ machine } }++ } @$data }; print Dumper $data; '
Re: Need help with removing duplicate hash keys from array of hashes
by chargrill (Parson) on Sep 25, 2006 at 04:41 UTC

    I'd be happy to help you figure out why your code isn't working. Let's have a look at what you've tried.



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
    A reply falls below the community's threshold of quality. You may see it by logging in.