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

Hello Monks, I need some help regarding a very petty issue of merging two multidimensional array for printing later. Here is a basic snippet of the code:
use strict; use Spreadsheet::WriteExcel; use Data::Dumper; my $workbook = Spreadsheet::WriteExcel->new("write_arrays.xls"); my $worksheet1 = $workbook->add_worksheet('Example 1'); my @arr = ( [ 'A' , 1, 0, 5, 6 ], [ 'B' , 3 , 4 , 5 , 6 ], [ 'C' , 2 , 4 ,3 ,5 ], [ 'D' , 6 , 7 , 8 ,8 ] ); my @arr1 = ( [ 'E' , 8 , 8 , 8 , 5 ], [ 'F' , 4 , 3 , 2 ,2 ], [ 'G' , 1 , 8 , 8, 8 ], [ 'H' , 1 , 4 , 5, 6 ] ); my @new; push(@arr, [@arr1]); $worksheet1->write_col('A3', \@arr);
As you can see, the merged array is 'misalinged' in the excel worksheet. I would like to preserve the order of arrays while printing. Also, I need to merge several ( read 5 to 6) such 2D arrays. Is there a succint way to achieve this ? Thanks for the help in advance !!

Replies are listed 'Best First'.
Re: How to merge/join two multidimensional(2D) arrays
by AppleFritter (Vicar) on Jul 04, 2014 at 16:09 UTC

    Well, you could do this:

    $worksheet1->write_col('A3', [@arr, @arr1]);

    since merging two arrays is as easy as putting them next to each other in list context (thanks to Perl's list-flattening). Alternatively, simply push the second array to the first, without taking a reference:

    push @arr, @arr1; $worksheet1->write_col('A3', \@arr);

    According to The Perl Cookbook (Recipe 4.9), the latter is more efficient. (Good book, BTW, that. I recommend it!)

Re: How to merge/join two multidimensional(2D) arrays
by davido (Cardinal) on Jul 04, 2014 at 18:00 UTC

    The Excel portion of your question is not related to the merging of two data structures, and might just be distracting you from solving the actual problem. Solve the first problem (the merging) first.

    When you say "merge", you mean one of two things; this:

    my @merged = ( \@arr, \@arr1 );

    ...or this...

    my @merged = ( @arr, @arr1 );

    You probably are after that second option: my @merged = ( @arr, @arr1 );. Currently you have this:

    my @merged = ( @arr, \@arr1 );

    ...which is probably wrong due to its asymmetry.

    Once you determine whether you need the first "merge" or the second one, you should then be able to populate the worksheet like this:

    $worksheet1->write_col('A3',\@merged);

    UPDATE: After looking at your previous post: Simplest way to match/filter 2d array of values to search in perl, I'm wondering if this is what you're doing:

    1. Start with a 2d array (possibly extracted from a worksheet).
    2. Find all the rows containing some element greater than a certain value.
    3. Derive two arrays; one with rows that passed the threshold, and one containing the remainder, which didn't pass the threshold.
    4. Join the two arrays (which is why, in this question, you wanted to preserve the order).
    5. Write the structure out to a workbook.

    If this sounds familiar, what you are doing is called partitioning. A very capable module for partitioning in Perl is Sort::Key::Top. You might look into it to see if it helps simplify your solution.


    Dave

Re: How to merge/join two multidimensional(2D) arrays
by ww (Archbishop) on Jul 04, 2014 at 17:46 UTC