G'day monkini,
Some points on the code you posted:
-
In scalar context, @array_name evaluates to the number of elements in that array. Your condition would have been better as ($j < @AoA): it's easier to read and has one less calculation for every iteration.
$#array_name evaluates to the last index in the array: your code suggests you understand this.
The condition is actually unnecessary: explained below.
-
A for loop is almost always a better choice for iterating an array.
In this instance, the first two lines of code could have been replaced by just: for (@AoA) {
-
shift will modify @AoA and eventually remove all elements from it.
Did you want such a modification?
Did you consider the extra processing involving (in every iteration) with this modification?
-
$a and $b: the issue here has already been covered by kennethk (above).
-
Rather than using multiple statements each retrieving a single element, use a single statement that retrieves an array slice (see "perldata: Slices").
Putting all that together, the code you posted could have been written as:
for (@AoA) {
my ($x, $y) = @{$_}[1, 2];
}
Here's my test:
#!/usr/bin/env perl -l
use strict;
use warnings;
my @AoA = ([qw{0.93 a1 b1}], [qw{0.89 a2 b2}], [qw{0.88 a3 b3}],
[qw{0.87 a4 b4}], [qw{0.86 a5 b5}]);
for (@AoA) {
my ($x, $y) = @{$_}[1, 2];
# Now do something with the retrieved values, e.g.
print "$x $y";
}
Output:
a1 b1
a2 b2
a3 b3
a4 b4
a5 b5