Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: Add new data to array

by Anonymous Monk
on Mar 14, 2020 at 22:44 UTC ( [id://11114274]=note: print w/replies, xml ) Need Help??


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

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!

Replies are listed 'Best First'.
Re^5: Add new data to array
by AnomalousMonk (Archbishop) on Mar 14, 2020 at 23:12 UTC

    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' },

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found