Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Add new data to array

by swl (Parson)
on Mar 13, 2020 at 22:26 UTC ( [id://11114239]=note: print w/replies, xml ) Need Help??


in reply to Add new data to array

I'm guessing that you want to add the new values into the hashref that is $row, not to the arrayref that is $data?

If so then you can use slice assignment (untested):

my %new_row_data = (new_name => $name, new_ad1 => $ad1, new_City +=> $town, new_z_code => $zcode); @$row{keys %new_row_data} = values %new_row_data;

You could also loop over the keys of %new_row_data and add each key/value pair in turn if that's easier to follow. The slice is more efficient, though, so better for very large data sets.

my %new_row_data = (new_name => $name, new_ad1 => $ad1, new_City => +$town, new_z_code => $zcode); foreach my $key (keys %new_row_data) { $row->{$key} = $new_row_data{$key}; }

Replies are listed 'Best First'.
Re^2: Add new data to array
by Anonymous Monk on Mar 14, 2020 at 02:45 UTC
    I want to extract the values found in $row and add to all arrayrefs in $data.

      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:  <%-{-{-{-<

        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!
        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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114239]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found