in reply to Merging Data

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data1 = [ { 'NAME' => 'JOE DOE', 'DATE' => '2015-05-05', 'NUMBER' => '00001', }, ]; my $data2 = [ { 'ACCOUNT1' => '1b', 'ACCOUNT2' => '2b', 'ACCOUNT3' => '3b', 'ACCOUNT4' => '3d', }, ]; $data1->[0]{$_} = $data2->[0]{$_} for (keys %{$data2->[0]}); print Dumper $data1;

Replies are listed 'Best First'.
Re^2: Merging Data
by Anonymous Monk on Jun 10, 2015 at 20:05 UTC
    What happens if I don't know the size of $data1 and $data2?
    The way it is its only merging the first elements because it has a fixed size to them.

    Thanks!
      If you don't know the size, you can determine at run time by looking. You can always determine the size of an array. If all the referencing is not clear, dereference in multiple steps. Maybe read perldsc, try something new, and/or ask different questions.
      # Get the size of data1 my $sizeOfData1 = scalar @{$data1}; print $sizeOfData1; # Enumerate over each element, a hash reference, of data1 for my $hashref (@{$data1}) { # Dereference the array my %realHash = %{$hashref}; # Dereference the hash }
      Update Fixed code tags