I am trying to make an array of arrays and then reference them.
I make somethign like:
sub foobar
{
my @array;
my $i;
for ($i = 0; $i < 1000; $i=$i+1)
{
my @row;
$row[0] = $i;
$row[1] = foo($bar);
push @array , [@row];
}
return \@array;
}
I can get to the values via:
$array->[x]->[y];
However I don’t understand why the second -> is needed. $array->[x] I understand because $array is a reference. But isn’t $array->[x] meant to be an array? Why doesn’t this work:
my @notarray = $array->[x];
What exactally is not array filled with now? Because it certainly doesn’t seem to be an array containing $i , foo($bar)
How would $array->[x]->[y] be different for a reference to an array of references to arrays?
The second
->isn’t needed, actually.Here’s the deal: All Perl array values and hash values must be scalars. That means either a string, number, or array/hash reference (and not a plain old array or hash).
So the first
->operator dereferences the array and gets at the x’th row. In there is– not an array, but an array reference. So in order to get to the data in there, you’d theoretically need another->operator.But get this. Perl is smart: It knows that after one array or hash access, if another access happens, the only way this is possible is through an array/hash reference (because your first array/hash access MUST return a scalar)! So you don’t need the second arrow after all.
See perldata for more details.