Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi Monks!

I had a similar question a few days ago, but the test data I had wasn't right, so I am hopping to get another shot here, cause I am stuck.

First, add new rows to the data where "status = Main", I have that on the first "foreach loop", thanks!

Here is where I am stuck, adding new values from "status = Extra" into "status = Main" where ID in "status = Main" is equal the ID in "status = Extra".
So in this case, the end result I am trying to get would be like this,:

$VAR3 = { 'new_City' => 'BO', 'ID' => '2222', 'status' => 'Main', 'name' => 'John D', 'City' => 'NY', 'state' => 'UA', 'new_ad1' => '100 main St.', 'Ad1' => '20 North Central St.', 'new_z_code' => '0007', 'new_name' => 'Tony Star', 'zCode' => '0002' }; $VAR6 = { 'new_City' => 'MA', 'ID' => '1111', 'status' => 'Main', 'name' => 'Charles D', 'City' => 'NM', 'state' => 'CA', 'new_ad1' => '44 Dell St', 'Ad1' => '12th Street', 'new_z_code' => '9857', 'new_name' => 'Marie Doe', 'zCode' => '2334' }; # Noticed that this one didn't have any "status = Extra" associated wi +th it, so the new added rows are empty, its just fine like that. $VAR9 = { 'new_City' => '', 'ID' => '8888', 'status' => 'Main', 'name' => 'AKAKAK', 'City' => 'AA', 'state' => 'PP', 'new_ad1' => '', 'Ad1' => '1AAAA', 'new_z_code' => '', 'new_name' => '', 'zCode' => 'TTTT' }; <br><br>


Here is a sample code to show what I am trying to do:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data =[ { Ad1 => '20 North Central St.', status => 'Main', City => 'NY +', zCode => '0002', name => 'John D', ID => '2222', state => 'TO +' }, { Ad1 => '15 South Street', status => 'Property', City => 'NY +', zCode => '0002', name => 'John V', ID => '2222', state => 'TO +' }, { Ad1 => '100 main St.', status => 'Extra', City => 'BO +', zCode => '0007', name => 'Tony Star', ID => '2222', state => 'UA +' }, { 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 => 'Extra', 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' + }, { 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' }, ]; # Add new rows to data where status equal Main my %new_row; foreach my $get_data (@$data) { if ( $get_data->{ 'status' } eq 'Main' ){ # Add New rows %new_row = ( new_name => '', new_ad1 => '', new_City => '', new_z_code => '' ); @$get_data{keys %new_row} = values %new_row; } } # At this point all arrays with status equal "Main" has all the new ro +ws added with empty values. print Dumper @$data; # Now add new values from Extra into Main where ID in Main is equal ID + in Extra. # Not working, inserts the values into the rows, but over writes the v +alues. # Cant think on how to check for the IDs equality in status=Main and I +D in status=Extra. my %new_row_data; foreach my $getdata (@$data) { if ( $getdata->{ 'status' } eq 'Extra' ){ %new_row_data = ( status => 'Main', new_name => $getdata->{ 'name' }, new_ad1 => $getdata->{ 'Ad1' }, new_City => $getdata->{ 'City' }, new_z_code => $getdata->{ 'zCode' } ); @$getdata{keys %new_row_data} = values %new_row_data; } } print Dumper @$data; exit;

Thanks once again for looking!!

In reply to Add new rows and data to array. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found