craret has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise ones... I am stuck after a day of brain crunching code :) I require the wisdom of the monks to assist me in the trivial quest of merging 2 2d arrays... The arrays are as follows:
$VAR1 = [ [ '111', '222' ] ]; $VAR2 = [ [ '333', '444' ], [ '555', '666' ], ];
What im looking for is:
$VAR1 = [ [ '111', '222' ], [ '333', '444' ], [ '555', '666' ], ];
Thanks in advance....

Replies are listed 'Best First'.
Re: Merge 2 2d arrays
by Fletch (Bishop) on Feb 14, 2008 at 14:56 UTC

    Well, there's the simple answer of just use push (push @{$VAR1}, @{$VAR2}), but that's ignoring the fact that's only a shallow copy of the constituent arrayrefs and that you'll be able to diddle (say) $VAR2->[1]->[1] and affect the value you see as $VAR1->[2]->[1]. If that's an issue you'd want to iterate over @{ $VAR2 } and push copies instead (push @{$VAR1}, [ @{ $_ } ] for @{ $VAR2 }). Or use something like Storable's dclone routine to do a deep clone of $VAR2 and use the first push solution.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Merge 2 2d arrays
by kyle (Abbot) on Feb 14, 2008 at 16:05 UTC
Re: Merge 2 2d arrays
by starbolin (Hermit) on Feb 14, 2008 at 18:04 UTC

    It is helpful to remember that your Array of Arrays is just an array of references. So it is only necessary to pop the references from one and push them on to the other. There is no need to copy the actual data. This:

    push @$VAR1, pop @$VAR2;

    ... moves one array from VAR2 into VAR1. You can extend that with a simple map or foreach


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Merge 2 2d arrays
by apl (Monsignor) on Feb 14, 2008 at 15:55 UTC
    What did you try to do, in order to merge them? Can you show us the code you wrote that didn't work? Perhaps we could point you in the direction of how to fix it.
      Thanks all for the replies! Ok, like apl says, maybe i should be a bit more specific :) Let me explain the scenario first... I am building a PDF from data that I extract from a DB. When the data gets retrieved via a DBI fetchall_arrayref, it only returns the data like it should. The problem is that I am using PDF::Table. I need to add a header row to the array cause PDF::Table auto repeats the first row as a header if the data spans more than 1 page. So keeping that in mind this is the scenario:
      ... use Data::Dumper; ... my @ref_header = ( [ ["Name", "Surname"] ], ); ... @ref_db_data = $sth->fetchall_arrayref;
      the data that is in @ref_db_data is:
      print Dumper @ref_db_data; $VAR2 = [ [ 'John', 'Doe' ], [ 'Jane', 'Smith' ], ];
      What I want to do is joint (merge) the two arrays to simply add the header row so that I can then print it on the PDF. Hope this makes sense... :P

        Your @ref_db_data is going to be a one-element array with an array reference as that one element. There's nothing wrong with that, but it might not be what you really want.

        I'm guessing what you're shooting for is this:

        my $ref_header = [ 'Name', 'Surname' ]; my @ref_db_data = @{ $sth->fetchall_arrayref }; unshift @ref_db_data, $ref_header;

        Or maybe this:

        my $ref_header = [ 'Name', 'Surname' ]; my $ref_db_data = $sth->fetchall_arrayref; unshift @{ $ref_db_data }, $ref_header;

        Here's your original situation:

        my @ref_header = ( [ ["Name", "Surname"] ], ); ... @ref_db_data = $sth->fetchall_arrayref; unshift @{$ref_db_data[0]}, $ref_header[0]->[0];

        ...but I'm not sure if that's what you're shooting for. You wind up with something like:

        @ref_db_data = ( [ [ 'Name', 'Surname' ], [ 'John', 'Doe' ], [ 'Jane', 'Smith' ] ] );