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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.