in reply to Multi-dimensional arrays...

Basically I want a 9 by 9 matrix that hold an array instead of just a scaler.

for my $row (0..8) { for my $col (0..8) { my @record = ...; $data[$row][$col] = \@record; } }

If the above "..." is built using a loop, you can avoid the intermediary variable @record:

for my $row (0..8) { for my $col (0..8) { for my $x (...) { push @{ $data[$row][$col] }, $x; } } }

Replies are listed 'Best First'.
Re^2: Multi-dimensional arrays...
by nothign (Novice) on May 31, 2009 at 18:38 UTC

    Wouldn't this solution make all elements of the double array point to the same reference, which means the same array?

    What if they need to each have there own separate array?

      No, the my @record = ...; inside the inner loop creates a new lexically scoped array each iteration. Consider the following code.

      $ perl -Mstrict -wle ' > my @arr; > foreach my $iter ( 1 .. 3 ) > { > my @rec = map { $_ * $iter } ( 1, 2, 3 ); > push @arr, \ @rec; > } > print qq{$_: @$_} for @arr;' ARRAY(0x22aa8): 1 2 3 ARRAY(0x22b14): 2 4 6 ARRAY(0x22b80): 3 6 9 $

      Note how the references are different for each element of @arr and the contents reflect the value of $iter each time through the loop.

      I hope this is helpful.

      Cheers,

      JohnGG

        Thank you for the further clarification, excuse my ignorance. And many thanks :D