for my $i (0..$#scores) { print $scores[$i], "\n"; }
for my $score (@scores) { print $score, "\n"; }
Or even print for @scores; (after perl -l or $/ = "\n";; or just say for @scores; in Perl 5.10 or later after use feature 'say';).
It may not be worth mentioning, but the test $i <= $#scores should probably be considered an excessive use of the special variable $#foo. Because of its complexity (it's an lvalue, among other things), there can be performance implications in repeatedly accessing or setting it. In this case it's moot, since the reasonable way to access the array's elements is to iterate over each element or each index (the latter results in it being used only wants). But don't avoid a sensible construct out of hand without considering benchmarks and input from Monks past and present -- it's likely niggling points like this will have no significance in a larger program that hasn't been profiled.
In reply to Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by Anonymous Monk
in thread Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by filesurfer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |