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

Need help with removing duplicate hash keys from array of hashes

by Anonymous Monk
on Sep 25, 2006 at 04:33 UTC ( [id://574664]=perlquestion: print w/replies, xml ) Need Help??

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 (Abbot) 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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found