in reply to Re^4: Add new rows and data to array.
in thread Add new rows and data to array.
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
|
|---|