Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Add new rows and data to array.

by clueless newbie (Curate)
on Mar 19, 2020 at 22:15 UTC ( [id://11114490] : note . print w/replies, xml ) Need Help??


in reply to Add new rows and data to array.

Indexing first might make things easier.

#!/usr/bin/perl use Data::Alias; use Data::Dumper; use 5.01800; use warnings; use Data::Dumper; my $data =[ { Ad1 => '20 North Central St.', status => 'Main', City => 'NY +', zCode => '0002', name => 'John D', ID => '2222', state => 'T +O' }, { Ad1 => '15 South Street', status => 'Property', City => 'NY +', zCode => '0002', name => 'John V', ID => '2222', state => 'T +O' }, { Ad1 => '100 main St.', status => 'Extra', City => 'BO +', zCode => '0007', name => 'Tony Star', ID => '2222', state => 'U +A' }, { Ad1 => '5540 Chelsea Avenue', status => 'Cabin', City => 'NE +', zCode => '4562', name => 'Carly Simon', ID => '2222', state => 'T +O' }, { Ad1 => '12th Street', status => 'Main', City => 'NM +', zCode => '2334', name => 'Charles D', ID => '1111', state => 'CA +' }, { Ad1 => '44 Dell St', status => 'Extra', City => 'MA +', zCode => '9857', name => 'Marie Doe', ID => '1111', state => 'C +A' }, { Ad1 => '33 Dust Road', status => 'Property', City => 'ET +', zCode => '3345', name => 'Chapim Thor',ID => '1111', state => 'CA +' }, { Ad1 => '01 Charles St', status => 'Cabin', City => 'CA +', zCode => '2334', name => 'Claud Odur', ID => '1111', state => 'CA +' }, { Ad1 => '1AAAA', status => 'Main', City => 'AA', zCode => 'T +TTT', name => 'AKAKAK', ID => '8888', state => 'PP' }, { Ad1 => '2AAAA', status => 'Test', City => 'BB', zCode => 'W +WWW', name => 'BXBXBX', ID => '8888', state => 'HH' }, { Ad1 => 'CCCCC', status => 'Property', City => 'ET', zCode => '3 +345', name => 'Chapim Thor', ID => '8888', state => 'CA' }, { Ad1 => 'DDDDD', status => 'Cabin', City => 'CA', zCode => '2 +334', name => 'Claud Odur', ID => '8888', state => 'CA' }, ]; my $index={}; for (@$data) { if ($_->{status} eq 'Main') { $index->{$_->{ID}}{Main}=$_; } elsif ($_->{status} eq 'Extra') { $index->{$_->{ID}}{Extra}=$_; }; }; $Data::Dumper::Sortkeys=1; warn Data::Dumper->Dump([\$data,\$index],[qw(*data *index)]),' '; for my $key (keys %$index) { if (exists $index->{$key}{Main} && exists $index->{$key}{Extra}) { # We have a candidate for merging alias my $candidate=$index->{$key}; warn Data::Dumper->Dump([\$candidate],[qw(*candidate)]),' '; for my $item (keys %{$candidate->{Extra}}) { # next for items you don't want moved #next # if ($item =~ m{status}); $candidate->{Main}{"new_$item"}=delete $candidate->{Extra +}{$item}; }; }; }; warn Data::Dumper->Dump([\$data],[qw(*data)]),' ';

yields:

$data = \[ { 'Ad1' => '20 North Central St.', 'City' => 'NY', 'ID' => '2222', 'name' => 'John D', 'state' => 'TO', 'status' => 'Main', 'zCode' => '0002' }, { 'Ad1' => '15 South Street', 'City' => 'NY', 'ID' => '2222', 'name' => 'John V', 'state' => 'TO', 'status' => 'Property', 'zCode' => '0002' }, { 'Ad1' => '100 main St.', 'City' => 'BO', 'ID' => '2222', 'name' => 'Tony Star', 'state' => 'UA', 'status' => 'Extra', 'zCode' => '0007' }, { 'Ad1' => '5540 Chelsea Avenue', 'City' => 'NE', 'ID' => '2222', 'name' => 'Carly Simon', 'state' => 'TO', 'status' => 'Cabin', 'zCode' => '4562' }, { 'Ad1' => '12th Street', 'City' => 'NM', 'ID' => '1111', 'name' => 'Charles D', 'state' => 'CA', 'status' => 'Main', 'zCode' => '2334' }, { 'Ad1' => '44 Dell St', 'City' => 'MA', 'ID' => '1111', 'name' => 'Marie Doe', 'state' => 'CA', 'status' => 'Extra', 'zCode' => '9857' }, { 'Ad1' => '33 Dust Road', 'City' => 'ET', 'ID' => '1111', 'name' => 'Chapim Thor', 'state' => 'CA', 'status' => 'Property', 'zCode' => '3345' }, { 'Ad1' => '01 Charles St', 'City' => 'CA', 'ID' => '1111', 'name' => 'Claud Odur', 'state' => 'CA', 'status' => 'Cabin', 'zCode' => '2334' }, { 'Ad1' => '1AAAA', 'City' => 'AA', 'ID' => '8888', 'name' => 'AKAKAK', 'state' => 'PP', 'status' => 'Main', 'zCode' => 'TTTT' }, { 'Ad1' => '2AAAA', 'City' => 'BB', 'ID' => '8888', 'name' => 'BXBXBX', 'state' => 'HH', 'status' => 'Test', 'zCode' => 'WWWW' }, { 'Ad1' => 'CCCCC', 'City' => 'ET', 'ID' => '8888', 'name' => 'Chapim Thor', 'state' => 'CA', 'status' => 'Property', 'zCode' => '3345' }, { 'Ad1' => 'DDDDD', 'City' => 'CA', 'ID' => '8888', 'name' => 'Claud Odur', 'state' => 'CA', 'status' => 'Cabin', 'zCode' => '2334' } ]; $index = \{ '1111' => { 'Extra' => ${$data}->[5], 'Main' => ${$data}->[4] }, '2222' => { 'Extra' => ${$data}->[2], 'Main' => ${$data}->[0] }, '8888' => { 'Main' => ${$data}->[8] } }; at 11114482.t line 41. $candidate = \{ 'Extra' => { 'Ad1' => '100 main St.', 'City' => 'BO', 'ID' => '2222', 'name' => 'Tony Star', 'state' => 'UA', 'status' => 'Extra', 'zCode' => '0007' }, 'Main' => { 'Ad1' => '20 North Central St.', 'City' => 'NY', 'ID' => '2222', 'name' => 'John D', 'state' => 'TO', 'status' => 'Main', 'zCode' => '0002' } }; at 11114482.t line 47. $candidate = \{ 'Extra' => { 'Ad1' => '44 Dell St', 'City' => 'MA', 'ID' => '1111', 'name' => 'Marie Doe', 'state' => 'CA', 'status' => 'Extra', 'zCode' => '9857' }, 'Main' => { 'Ad1' => '12th Street', 'City' => 'NM', 'ID' => '1111', 'name' => 'Charles D', 'state' => 'CA', 'status' => 'Main', 'zCode' => '2334' } }; at 11114482.t line 47. $data = \[ { 'Ad1' => '20 North Central St.', 'City' => 'NY', 'ID' => '2222', 'name' => 'John D', 'new_Ad1' => '100 main St.', 'new_City' => 'BO', 'new_ID' => '2222', 'new_name' => 'Tony Star', 'new_state' => 'UA', 'new_status' => 'Extra', 'new_zCode' => '0007', 'state' => 'TO', 'status' => 'Main', 'zCode' => '0002' }, { 'Ad1' => '15 South Street', 'City' => 'NY', 'ID' => '2222', 'name' => 'John V', 'state' => 'TO', 'status' => 'Property', 'zCode' => '0002' }, {}, { 'Ad1' => '5540 Chelsea Avenue', 'City' => 'NE', 'ID' => '2222', 'name' => 'Carly Simon', 'state' => 'TO', 'status' => 'Cabin', 'zCode' => '4562' }, { 'Ad1' => '12th Street', 'City' => 'NM', 'ID' => '1111', 'name' => 'Charles D', 'new_Ad1' => '44 Dell St', 'new_City' => 'MA', 'new_ID' => '1111', 'new_name' => 'Marie Doe', 'new_state' => 'CA', 'new_status' => 'Extra', 'new_zCode' => '9857', 'state' => 'CA', 'status' => 'Main', 'zCode' => '2334' }, {}, { 'Ad1' => '33 Dust Road', 'City' => 'ET', 'ID' => '1111', 'name' => 'Chapim Thor', 'state' => 'CA', 'status' => 'Property', 'zCode' => '3345' }, { 'Ad1' => '01 Charles St', 'City' => 'CA', 'ID' => '1111', 'name' => 'Claud Odur', 'state' => 'CA', 'status' => 'Cabin', 'zCode' => '2334' }, { 'Ad1' => '1AAAA', 'City' => 'AA', 'ID' => '8888', 'name' => 'AKAKAK', 'state' => 'PP', 'status' => 'Main', 'zCode' => 'TTTT' }, { 'Ad1' => '2AAAA', 'City' => 'BB', 'ID' => '8888', 'name' => 'BXBXBX', 'state' => 'HH', 'status' => 'Test', 'zCode' => 'WWWW' }, { 'Ad1' => 'CCCCC', 'City' => 'ET', 'ID' => '8888', 'name' => 'Chapim Thor', 'state' => 'CA', 'status' => 'Property', 'zCode' => '3345' }, { 'Ad1' => 'DDDDD', 'City' => 'CA', 'ID' => '8888', 'name' => 'Claud Odur', 'state' => 'CA', 'status' => 'Cabin', 'zCode' => '2334' } ]; at 11114482.t line 53.

Note the now empty (formerly Extra) hashes resulting from the use of delete.