in reply to Re^2: Question about returning array from sub
in thread Question about returning array from sub
You still don't quite have the concepts. There is only a "2-D array" in Perl. See a post from today from Monk::Thomas with some awesome ASCII art depicting Perl arrays.
At the end you said: I return the array of array of references ...
Should be: "the array of array references" or "the array of arrayrefs" or "the array of references to arrays" ...
As for your latest code: (first I would recommend testing with a minimal script like the one I posted above. Then when you have it figured out, start plugging your own code/data back in)
@rot_mat
$rot_mat[0][0] = 'foo';
return \@rot_mat;
my $aref = &first_sub();
my $bar = $aref->[0]->[0]; # hard to read
my $baz = $aref->[0]; my $quux = $aref->[1]; # now $baz is a reference to the anonymous array Perl created # for the first element of @rot_mat when you assigned a value # to $rot_mat[0][0] in the first sub say 'yep' if $baz->[0] eq 'foo';
foreach my $anon_array ( @{ $aref } ) { # dereference the main array foreach my $value ( @{ $anon_array } ) { # dereference the anon arra +y say 'yep' if $value eq 'foo'; } }
Hope this helps.
|
|---|