#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11133140 use warnings; 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 %lastid; my @test; for my $this ( @$test_data ) { if( my $previous = $lastid{ $this->{ID} } ) { $this->{$_} eq '' or $previous->{$_} = $this->{$_} for keys %$this; } else { push @test, $lastid{ $this->{ID} } = { %$this } } } use Data::Dump 'dd'; dd 'test_data', $test_data, 'test', \@test;