in reply to Re: Merge 2 2d arrays
in thread Merge 2 2d arrays

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

Replies are listed 'Best First'.
Re^2: Merge 2 2d arrays
by kyle (Abbot) on Feb 14, 2008 at 21:14 UTC

    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' ] ] );
      Thank you Kyle!!!!!!!!
      unshift @{$ref_db_data[0]}, $ref_header[0]->[0];
      this is all I needed to fix my array :) This is proof that the knowledge of the monks are supreme!!!!