Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^5: Add new rows and data to array.

by BillKSmith (Monsignor)
on Mar 21, 2020 at 14:38 UTC ( [id://11114533]=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114533]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found