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($,,$;,$*,$/)
| [reply] [d/l] [select] |
Thanks a lot for the solution
| [reply] |
# Create a lookuptable to store found items
my %hash = ();
# Iterate over the data-array (thats what grep does here), and add the
+ item to newdata, and set the found item in the lookup table. When th
+e new item is in the lookup table, don't add it again.
my @newdata = grep { ! $hash{ $_->{operation} . $_->{machine} }++ } @$
+data;
# more readable than grep (to my opinion,probably not to all others :)
+ ), but basically the same:
my %hash = ();
my @newdata;
foreach my $item (@{$data}) {
if (! $hash{ $item->{operation} . $item->{machine} } ) {
$hash{ $item->{operation} . $item->{machine} } = 1 ;
push @newdata, $item;
}
}
"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
| [reply] [d/l] |