in reply to Combining two references

Through the magic of List::MoreUtils:
use Modern::Perl qw/2015/; use Data::Dump qw/dump/; use List::MoreUtils qw/zip natatime/; my $data1 = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'NUMBER' => '00001', }, { 'NAME' => 'ANTHONY RD', 'DATE' => '2012-01-07', 'NUMBER' => '00003', }, ]; my $data2 = [ { 'CAR1' => '1', 'CAR2' => '2', 'CAR3' => '3', }, { 'CAR1' => '1b', 'CAR2' => '2b', 'CAR3' => '3b', }, ]; my @all; my $it = natatime 2, zip @$data1, @$data2; while (my ($first, $second) = $it->()) { push @all, {%$first, %$second}; } say dump @all;
Output:
( { CAR1 => 1, CAR2 => 2, CAR3 => 3, DATE => "2009-05-05", NAME => "PAUL DY", NUMBER => "00001", }, { CAR1 => "1b", CAR2 => "2b", CAR3 => "3b", DATE => "2012-01-07", NAME => "ANTHONY RD", NUMBER => "00003", }, )

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Combining two references
by Anonymous Monk on Jun 10, 2015 at 14:32 UTC
    What if you just want to merge these two arrays:

    ... my $data1 = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'NUMBER' => '00001', }, ]; my $data2 = [ { 'CAR1' => '1b', 'CAR2' => '2b', 'CAR3' => '3b', 'CAR4' => '3d', }, ]; ...

    into:
    my $data3 = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'NUMBER' => '00001', 'CAR1' => '1b', 'CAR2' => '2b', 'CAR3' => '3b', 'CAR4' => '3d', }, ];

    Thanks!
      Why don't you try and see what it does?

      I'll wait while you try. ......

      See, it "just worked". Ain't Perl nice?

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
        I know it works using the module:
        List::MoreUtils qw/zip natatime/;

        Can it be done without it?

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data1 = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'NUMBER' => '00001', }, ]; my $data2 = [ { 'CAR1' => '1b', 'CAR2' => '2b', 'CAR3' => '3b', 'CAR4' => '3d', }, ]; my $new_data; push @{ $new_data }, $data1,$data2; print Dumper $new_data;

        And get this,
        I couldn't:
        my $data3 = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'NUMBER' => '00001', 'CAR1' => '1b', 'CAR2' => '2b', 'CAR3' => '3b', 'CAR4' => '3d', }, ];