in reply to Re^3: Add new rows and data to array.
in thread Add new rows and data to array.

Thank you, I am going to implement my code here now!!!

Replies are listed 'Best First'.
Re^5: Add new rows and data to array.
by BillKSmith (Monsignor) on Mar 21, 2020 at 14:38 UTC
    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

    Bill