Now that I understand your requirements, I can suggest a faster algorithm. Replace the temporary array @extras with a temporary hash. Use ID values as hash keys. An inner loop to search for a match is no longer necessary - just use a hash look-up.
use strict;
use warnings;
my @data = [
... # same as before
];
my %extras;
foreach (@$data) {
$extras{$_->{ID}} = $_ if $_->{status} eq 'Extra';
}
foreach my $main (@$data) {
next if $main->{status} ne 'Main';
# Autovivify new keys with default values
@{$main}{qw/new_name new_ad1 new_city new_z_code/}
= ('', '', '', '');
if (exists $extras{$main->{ID}}) {
# Update values with 'extras' if they exist
@{$main}{qw/new_name new_ad1 new_city new_z_code/}
= @{$extras{$main->{ID}}}{qw/name Ad1 City zCode/};
}
print Dumper($main);
}
OUTPUT: Same as in Re^5: Add new rows and data to array. above