Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by BillKSmith (Monsignor)
on Mar 20, 2020 at 00:16 UTC ( [id://11114494]=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.

I think that you now want to add the four new keys (with the value of each initialized to the null string) to every hash which has a status of 'Main'. Update the values if they are available in an 'extra' hash.
... # As before my @extras = grep {$_->{status} eq 'Extra'} @$data; my @mains = grep {$_->{status} eq 'Main' } @$data; MAIN: foreach my $main (@mains) { @{$main}{qw/new_name new_ad1 new_city new_z_code/} = ('', '', '', ''); EXTRA: foreach my $extra (@extras) { next EXTRA if $main->{ID} ne $extra->{ID}; @{$main}{qw/new_name new_ad1 new_city new_z_code/} = @{$extra}{qw/name Ad1 City zCode/}; last EXTRA; } print Dumper($main); }

OUTPUT:

$VAR1 = { 'City' => 'NY', 'new_city' => 'BO', 'new_ad1' => '100 main St.', 'Ad1' => '20 North Central St.', 'new_name' => 'Tony Star', 'zCode' => '0002', 'state' => 'TO', 'status' => 'Main', 'new_z_code' => '0007', 'name' => 'John D', 'ID' => '2222' }; $VAR1 = { 'City' => 'NM', 'new_city' => 'MA', 'new_ad1' => '44 Dell St', 'Ad1' => '12th Street', 'state' => 'CA', 'zCode' => '2334', 'status' => 'Main', 'new_name' => 'Marie Doe', 'new_z_code' => '9857', 'name' => 'Charles D', 'ID' => '1111' }; $VAR1 = { 'new_ad1' => '', 'City' => 'AA', 'new_city' => '', 'Ad1' => '1AAAA', 'new_name' => '', 'status' => 'Main', 'state' => 'PP', 'zCode' => 'TTTT', 'name' => 'AKAKAK', 'ID' => '8888', 'new_z_code' => '' };
Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found