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

I have two arrays @array1 and @array2. I want to create two dimensional array from these. ie first column will be array1 and second will be array 2 How can I do it ?

Replies are listed 'Best First'.
Re: Creating two dimensional array
by kyle (Abbot) on Oct 30, 2007 at 21:53 UTC
    use List::Util qw( max ); my @two_dim = map { [ $array1[$_], $array2[$_] ] } 0 .. max( $#array1, $#array2 );

    This assumes you have List::Util (a safe assumption). If one array is longer than the other, the "missing" entries will be undef.

      Cute use of "max" there.

      I've been getting into "pairwise" lately, myself. From the CPAN module List::MoreUtils:

      use Data::Dumper; use List::MoreUtils qw(pairwise); my @array1 = qw( a b c d e f ); my @array2 = qw( 1 2 3 4 5 6 ); my @matrix = pairwise { [$a, $b] } @array1, @array2; print Dumper(\@matrix), "\n";
Re: Creating two dimensional array
by Fletch (Bishop) on Oct 30, 2007 at 21:49 UTC
Re: Creating two dimensional array
by ikegami (Patriarch) on Oct 30, 2007 at 22:12 UTC
    The following will do the trick:
    my @matrix = (\@array1, \@array2);

    It can be accessed using

    $matrix[$col][$row]
      This is what I wanted to post, but I was thinking that maybe I had misunderstood the question. I use this exact thing to 'concat' cols together into a "2 dim array".
      --
      I used to drive a Heisenbergmobile, but everyone I looked at the speedometer, I got lost.
      I think you just answered a slightly different question. Most of us have interpreted the goal like this:
      $VAR1 = [ [ 'a', '1' ], [ 'b', '2' ], [ 'c', '3' ] ];
      Your solution looks like this:
      $VAR1 = [ [ 'a', 'b', 'c' ], [ '1', '2', '3' ] ];

        Most of us have interpreted the goal like this

        Yes, I noticed. It would have been silly to post something that was already posted.

        There's no rule that says that the first dimension must be rows and the second must be columns. Here code that shows my solution does indeed use @array1 as the first column and @array2 as the second column.

        my @array1 = qw( a b c ); my @array2 = qw( X Y Z ); my @matrix = (\@array1, \@array2); for my $row (0..$#{$matrix[0]}) { for my $col (0..$#matrix) { print $matrix[$col][$row], "\t"; } print "\n"; }
        a X b Y c Z

        Update: Added code.

Re: Creating two dimensional array
by swampyankee (Parson) on Oct 30, 2007 at 21:56 UTC

    Why do you want to do this and what have you tried?

    In any case, Perl does not have true two dimensional arrays; it has one dimensional arrays where the elements may be references to arrays; see for example perlref).


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.

Re: Creating two dimensional array
by doom (Deacon) on Oct 30, 2007 at 21:59 UTC
    Well, you could hire me. $100/hr. Or you could take a class in perl programming, or something.

    Something like this works (I presume both arrays are of equal length):

    my @matrix; for my $i (0 .. $#array1) { $matrix[$i] = [ $array1[ $i ], $array2[ $i ] ]; } use Test::More qw(no_plan); is( $matrix[1][1], $array2[1], "Spotcheck looks good." );