I think your code is doing something in reverse, noticed that I need to keep the values in the node where CODE => 10. But if I change the value in "Number" and run the code, it gets overwritten from the value in the node with "6":
use strict;
use warnings;
use Data::Dumper;
my $test_data = [
{'ID' => '1212', 'Name' => 'JOE' , 'Number' => 'XY1', 'CODE' =>
+'6',},
{'ID' => '1212', 'Name' => '' , 'Number' => 'WW3', 'CODE' => '
+10',},
{'ID' => '4456', 'Name' => 'MARIA', 'Number' => 'TYX', 'CODE' =>
+'6',},
{'ID' => '4456', 'Name' => '' , 'Number' => 'TYX', 'CODE' => '
+10',},
{'ID' => '8765', 'Name' => 'JEAN' , 'Number' => 'HPO', 'CODE' =>
+'6',},
];
my @result;
foreach my $data ( @{ $test_data } ) {
next if !$data->{Name};
my $id = $data->{ID};
push @result, $data;
foreach my $temp ( @{ $test_data } ) {
if (!$temp->{Name} and ($temp->{ID} eq $id)) {
$result[-1]->{CODE} = $temp->{CODE};
last;
}
}
}
print Dumper(\@result);
When it should be like this after running the code:
{'ID' => '1212', 'Name' => 'JOE' , 'Number' => 'WW3', 'CODE' => '
+10',},