Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Add new data to array

by AnomalousMonk (Bishop)
on Mar 14, 2020 at 05:50 UTC ( #11114245=note: print w/replies, xml ) Need Help??


in reply to Re^2: Add new data to array
in thread Add new data to array

Assuming that the "Sample Data" in the OP was produced by a statement like
    print Dumper @$data;
then there are no arrayrefs in $data; rather, $data is a reference to an array containing elements that are hash references.

The
    foreach my $row (@$data) { ... }
loop of the OPed code iterates over the elements of the array referenced by $data; these elements are hash references. The $row scalar is aliased in turn to each hash reference and thus becomes a hash reference.

The
    if ($row->{ 'status' } eq "houses") { ... }
conditional block within the for-loop of the OPed code suggests you want to extract certain values from each referenced hash for which  $row->{ 'status' } eq "houses" is true, and your statement here suggests you want to then add the most recently extracted set of values back into all of the hash references of the array. This doesn't seem to make any sense, but it can be done:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $data = [ { Ad1 => '20 SOUTH CENTRAL #B3', status => 'Property', City => 'NY +', zCode => '0002', name => 'John D' }, { Ad1 => '15 SOUTH CENTRAL #B4', status => 'Property', City => 'NY +', zCode => '0002', name => 'John V' }, { Ad1 => '100 main St.', status => 'houses', City => 'BO +', zCode => '0007', name => 'Mary' }, ]; ;; my %last_houses; ;; foreach my $hr_row (@$data) { if ($hr_row->{ 'status' } eq 'houses') { @last_houses { qw(new_name new_ad1 new_City new_z_code) } = @{ $hr_row }{ qw(name Ad1 City zCode ) }; } } ;; foreach my $hr_row (@$data) { %$hr_row = (%$hr_row, %last_houses); } ;; print Dumper $data; " $VAR1 = [ { 'new_City' => 'BO', 'status' => 'Property', 'name' => 'John D', 'City' => 'NY', 'Ad1' => '20 SOUTH CENTRAL #B3', 'new_ad1' => '100 main St.', 'new_z_code' => '0007', 'zCode' => '0002', 'new_name' => 'Mary' }, { 'new_City' => 'BO', 'status' => 'Property', 'name' => 'John V', 'City' => 'NY', 'Ad1' => '15 SOUTH CENTRAL #B4', 'new_ad1' => '100 main St.', 'new_z_code' => '0007', 'zCode' => '0002', 'new_name' => 'Mary' }, { 'new_City' => 'BO', 'status' => 'houses', 'name' => 'Mary', 'City' => 'BO', 'Ad1' => '100 main St.', 'new_ad1' => '100 main St.', 'new_z_code' => '0007', 'zCode' => '0007', 'new_name' => 'Mary' } ];
Again, please consider if this makes any sense.

Update: Also, please consider what output you would want if your input were

my $data = [ { Ad1 => '20 SOUTH CENTRAL #B3', status => 'Property', City => 'NY' +, zCode => '0002', name => 'John D' }, { Ad1 => '15 SOUTH CENTRAL #B4', status => 'Property', City => 'NY' +, zCode => '0002', name => 'John V' }, { Ad1 => '100 main St.', status => 'houses', City => 'BO' +, zCode => '0007', name => 'Mary' }, { Ad1 => '13 Terminal St.', status => 'houses', City => 'LA' +, zCode => '6664', name => 'Larry' }, ];


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Add new data to array
by Anonymous Monk on Mar 14, 2020 at 22:44 UTC
    The idea is strange, but the data in status => "Houses" needs to be in status => "Main" only and it isn't formatted like that in the database, so by using the magic of Perl, I was sure that it would be possible. This is what could work, unless it could be even more simplified.

    #!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; my $data =[ { Ad1 => '20 SOUTH CENTRAL #B3', status => 'Main', City => 'NY +', zCode => '0002', name => 'John D' }, { Ad1 => '15 SOUTH CENTRAL #B4', status => 'Property', City => 'NY +', zCode => '0002', name => 'John V' }, { Ad1 => '100 main St.', status => 'Houses', City => 'BO +', zCode => '0007', name => 'Mary Due' }, { Ad1 => '5540 Chelsea Avenue', status => 'Cabin', City => 'NE +', zCode => '4562', name => 'Carly Simon' }, ]; my %last_houses; foreach my $hr_row (@$data) { if ($hr_row->{ 'status' } eq 'Houses') { @last_houses { qw(new_name new_aA1 new_City new_zCode) } = @{ $hr_row }{ qw(name Ad1 City zCode ) }; } } foreach my $hr_row (@$data) { if ($hr_row->{ 'status' } eq 'Main') { %$hr_row = (%$hr_row, %last_houses); } } print Dumper $data; =code $VAR1 = [ { 'new_City' => 'BO', 'status' => 'Main', 'name' => 'John D', 'City' => 'NY', 'new_aA1' => '100 main St.', 'new_zCode' => '0007', 'Ad1' => '20 SOUTH CENTRAL #B3', 'zCode' => '0002', 'new_name' => 'Mary Due' }, { 'status' => 'Property', 'name' => 'John V', 'Ad1' => '15 SOUTH CENTRAL #B4', 'City' => 'NY', 'zCode' => '0002' }, { 'status' => 'Houses', 'name' => 'Mary Due', 'Ad1' => '100 main St.', 'City' => 'BO', 'zCode' => '0007' }, { 'status' => 'Cabin', 'name' => 'Carly Simon', 'Ad1' => '5540 Chelsea Avenue', 'City' => 'NE', 'zCode' => '4562' } ]; =cut


    Thank you!

      I'm glad that you have a working solution. Personally, I would hesitate to try to simplify it since I don't understand the rationale of what it's supposed to do. :)


      Give a man a fish:  <%-{-{-{-<

        When comes to data, sometimes there is no choice other than format it the way it will work for you, in this case, I needed to go through this nightmare to have it the way I needed.
        Now I only need to select this block with all the data I need, its just hard to explain sometimes.

        { 'new_City' => 'BO', 'status' => 'Main', 'name' => 'John D', 'City' => 'NY', 'new_aA1' => '100 main St.', 'new_zCode' => '0007', 'Ad1' => '20 SOUTH CENTRAL #B3', 'zCode' => '0002', 'new_name' => 'Mary Due' },
Re^4: Add new data to array
by Anonymous Monk on Mar 18, 2020 at 16:04 UTC
    You are correct when you asked about the other data set, since in reality the data is with much more data as in this small code sample, but just like your previous observation:

    my $data =[ { Ad1 => '20 SOUTH CENTRAL #B3', status => 'Main', City => 'NY +', zCode => '0002', name => 'John D', ID => '2222', state => 'TO' + }, { Ad1 => '15 SOUTH CENTRAL #B4', status => 'Property', City => 'NY +', zCode => '0002', name => 'John V', ID => '2222', state => 'TO' + }, { Ad1 => '100 main St.', status => 'Houses', City => 'BO +', zCode => '0007', name => 'Mary Due', ID => '2222', state => 'TO' + }, { Ad1 => '5540 Chelsea Avenue', status => 'Cabin', City => 'NE +', zCode => '4562', name => 'Carly Simon',ID => '2222', state => 'TO' + }, { Ad1 => '12th Street', status => 'Main', City => 'NM +', zCode => '2334', name => 'Charles D', ID => '1111', state => 'CA' + }, { Ad1 => '44 Dell St', status => 'Houses', City => 'MA +', zCode => '9857', name => 'Marie Doe', ID => '1111', state => 'CA' + }, { 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' + }, ];

    Now the results are confused:
    $VAR1 = [ { 'new_City' => 'MA', # Should be BO 'ID' => '2222', 'status' => 'Main', 'name' => 'John D', 'City' => 'NY', 'new_aA1' => '44 Dell St', # Should be 100 main St. 'state' => 'TO', 'new_zCode' => '9857', # Should be 0007 'Ad1' => '20 SOUTH CENTRAL #B3', 'new_name' => 'Marie Doe', # SHould be Mary Due 'zCode' => '0002' }, { 'new_City' => 'MA', 'ID' => '1111', 'status' => 'Main', 'name' => 'Charles D', 'City' => 'NM', 'new_aA1' => '44 Dell St', 'state' => 'CA', 'new_zCode' => '9857', 'Ad1' => '12th Street', 'new_name' => 'Marie Doe', 'zCode' => '2334' }, ];

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2020-06-07 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (42 votes). Check out past polls.

    Notices?