#!/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 => 'TTTT', name => 'AKAKAK', ID => '8888', state => 'PP' }, { Ad1 => '2AAAA', status => 'Test', City => 'BB', zCode => 'WWWW', name => 'BXBXBX', ID => '8888', state => 'HH' }, { Ad1 => 'CCCCC', status => 'Property', City => 'ET', zCode => '3345', name => 'Chapim Thor', ID => '8888', state => 'CA' }, { Ad1 => 'DDDDD', status => 'Cabin', City => 'CA', zCode => '2334', 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 rows 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 values. # Cant think on how to check for the IDs equality in status=Main and ID 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;