Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Add new rows and data to array.

by Anonymous Monk
on Mar 19, 2020 at 21:35 UTC ( [id://11114489]=note: print w/replies, xml ) Need Help??


in reply to Re: Add new rows and data to array.
in thread Add new rows and data to array.

Yes, but I also need the other "status = Main" with no Extra related:

This one, I will have lots of data in that situation:
$VAR9 = { 'new_City' => '', 'ID' => '8888', 'status' => 'Main', 'name' => 'AKAKAK', 'City' => 'AA', 'state' => 'PP', 'new_ad1' => '', 'Ad1' => '1AAAA', 'new_z_code' => '', 'new_name' => '', 'zCode' => 'TTTT' };

Replies are listed 'Best First'.
Re^3: Add new rows and data to array.
by BillKSmith (Monsignor) on Mar 19, 2020 at 22:21 UTC
    The $data array is updated. Print any part you want.
    ...# As before my @extras = grep {$_->{status} eq 'Extra'} @$data; my @mains = grep {$_->{status} eq 'Main' } @$data; MAIN: foreach my $main (@mains) { EXTRA: foreach my $extra (@extras) { next EXTRA if $main->{ID} ne $extra->{ID}; @{$main}{qw/new_name new_ad1 new_city new_z_code/} = @{$extra}{qw/name Ad1 City zCode/}; } print Dumper($main); }

    OUTPUT:

    $VAR1 = { 'state' => 'TO', 'new_z_code' => '0007', 'zCode' => '0002', 'status' => 'Main', 'ID' => '2222', 'name' => 'John D', 'City' => 'NY', 'new_city' => 'BO', 'new_ad1' => '100 main St.', 'Ad1' => '20 North Central St.', 'new_name' => 'Tony Star' }; $VAR1 = { 'zCode' => '2334', 'status' => 'Main', 'ID' => '1111', 'state' => 'CA', 'new_z_code' => '9857', 'Ad1' => '12th Street', 'new_name' => 'Marie Doe', 'name' => 'Charles D', 'City' => 'NM', 'new_city' => 'MA', 'new_ad1' => '44 Dell St' }; $VAR1 = { 'City' => 'AA', 'name' => 'AKAKAK', 'state' => 'PP', 'ID' => '8888', 'Ad1' => '1AAAA', 'zCode' => 'TTTT', 'status' => 'Main' };
    Bill
      Noticed that I still need to new rows, but empty because there is no status = extra related to that one :

      $VAR9 = { 'new_City' => '', <<<<<<< 'ID' => '8888', 'status' => 'Main', 'name' => 'AKAKAK', 'City' => 'AA', 'state' => 'PP', 'new_ad1' => '', <<<<<<< 'Ad1' => '1AAAA', 'new_z_code' => '', <<<<<< 'new_name' => '', <<<<<< 'zCode' => 'TTTT' };
        I think that you now want to add the four new keys (with the value of each initialized to the null string) to every hash which has a status of 'Main'. Update the values if they are available in an 'extra' hash.
        ... # As before my @extras = grep {$_->{status} eq 'Extra'} @$data; my @mains = grep {$_->{status} eq 'Main' } @$data; MAIN: foreach my $main (@mains) { @{$main}{qw/new_name new_ad1 new_city new_z_code/} = ('', '', '', ''); EXTRA: foreach my $extra (@extras) { next EXTRA if $main->{ID} ne $extra->{ID}; @{$main}{qw/new_name new_ad1 new_city new_z_code/} = @{$extra}{qw/name Ad1 City zCode/}; last EXTRA; } print Dumper($main); }

        OUTPUT:

        $VAR1 = { 'City' => 'NY', 'new_city' => 'BO', 'new_ad1' => '100 main St.', 'Ad1' => '20 North Central St.', 'new_name' => 'Tony Star', 'zCode' => '0002', 'state' => 'TO', 'status' => 'Main', 'new_z_code' => '0007', 'name' => 'John D', 'ID' => '2222' }; $VAR1 = { 'City' => 'NM', 'new_city' => 'MA', 'new_ad1' => '44 Dell St', 'Ad1' => '12th Street', 'state' => 'CA', 'zCode' => '2334', 'status' => 'Main', 'new_name' => 'Marie Doe', 'new_z_code' => '9857', 'name' => 'Charles D', 'ID' => '1111' }; $VAR1 = { 'new_ad1' => '', 'City' => 'AA', 'new_city' => '', 'Ad1' => '1AAAA', 'new_name' => '', 'status' => 'Main', 'state' => 'PP', 'zCode' => 'TTTT', 'name' => 'AKAKAK', 'ID' => '8888', 'new_z_code' => '' };
        Bill
      Thank you, I am going to implement my code here now!!!
        Now that I understand your requirements, I can suggest a faster algorithm. Replace the temporary array @extras with a temporary hash. Use ID values as hash keys. An inner loop to search for a match is no longer necessary - just use a hash look-up.
        use strict; use warnings; my @data = [ ... # same as before ]; my %extras; foreach (@$data) { $extras{$_->{ID}} = $_ if $_->{status} eq 'Extra'; } foreach my $main (@$data) { next if $main->{status} ne 'Main'; # Autovivify new keys with default values @{$main}{qw/new_name new_ad1 new_city new_z_code/} = ('', '', '', ''); if (exists $extras{$main->{ID}}) { # Update values with 'extras' if they exist @{$main}{qw/new_name new_ad1 new_city new_z_code/} = @{$extras{$main->{ID}}}{qw/name Ad1 City zCode/}; } print Dumper($main); }

        OUTPUT: Same as in Re^5: Add new rows and data to array. above

        Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found