February 2012
Intermediate to advanced
1184 pages
37h 17m
English
Here’s how to put together a two-dimensional array:
# Assign a list of array references to an array.
@AoA = (
[ "fred", "barney" ],
[ "george", "jane", "elroy" ],
[ "homer", "marge", "bart" ],
);
print $AoA[2][1]; # prints "marge"The overall list is enclosed by parentheses, not brackets, because you’re assigning a list and not a reference. If you wanted a reference to an array instead, you’d use brackets:
# Create a reference to an array of array references.
$ref_to_AoA = [
[ "fred", "barney", "pebbles", "bamm bamm", "dino", ],
[ "homer", "bart", "marge", "maggie", ],
[ "george", "jane", "elroy", "judy", ],
];
print $ref_to_AoA–>[2][3]; # prints "judy"Remember that there is an implied –> between every pair of adjacent
braces or brackets. Therefore, these two lines:
$AoA[2][3] $ref_to_AoA–>[2][3]
are equivalent to these two lines:
$AoA[2]–>[3] $ref_to_AoA–>[2]–>[3]
There is, however, no implied –> before the first pair of brackets,
which is why the dereference of $ref_to_AoA requires the initial –>. Also remember that you can count
backward from the end of an array with a negative index, so:
$AoA[0][–2]
is the next-to-last element of the first row.
Read now
Unlock full access