in reply to Question about returning array from sub

@rot_mat is an array of array references, that's why you see three fields in the output from your print statement. If you wanted to print all values of $rot_mat[0] then you would need to do

print @{$rot_mat[0]};

or just use Data::Dumper on \@rot_mat to get the full tree. See perldsc for more on these nested structures and the section "Access and Printing of an ARRAY OF ARRAYS" for this particular question.

Replies are listed 'Best First'.
Re^2: Question about returning array from sub
by ojagan (Novice) on Jul 10, 2015 at 12:48 UTC
    Thanks hippo. That indeed did work. But it is still not clear to me. I did declare the array and assign values to it and not references. So when I want to print the array why does it print the array with references.
      Perl does multidimensional arrays by storing elements of the sub-array in the parent array. So
      $x[0][1] = 12;

      is the syntactic-sugar equivalent of

      $x[0]->[1] = 12;

      (IIRC, in Perl 4, this was the required invocation.)

      And the whole sub-array as

      $x[0] = \@subarray;

      You might also think about it like this:

      @x = ( ["some", "sub", "array"], ["next", "sub", "array] );

      which is making anonymous arrays, and assigning the lot to @x. And these can be nested as deeply as you have memory for (AFAIK, but that would be a good exercise to check).

      Update:

      Yes, it seems that simply nesting empty arrays keeps going until it runs out of memory, starts swapping, and in my case, is killed by the OS.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of