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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.