should probably avoid using the $a and $b variables, which have a special purpose (for sorting).for ($a=0; $a<=$#columns; $a++) { for ($b=0; $b<=$#columns; $b++) { print "$list[$a][$b] "; # for testing } print " \n"; }
In general, you should probably try to avoid using the C-style for loop and array subscripts when you can, because it can be made significantly simpler and easier by iterating directly over the values (no off-by-one errors, no out-of-range errors).
which prints:my @AoA = ([1, 2, 3], [4, 5, 6], [7, 8, 9]); # an array of arrays for +testing purpose for my $row (@AoA) { for my $col (@$row) { print "$col "; } print "\n"; }
1 2 3 4 5 6 7 8 9
And if you need to make some calculations:
which will print the three computed averages:my @AoA = ([1, 2, 3], [4, 5, 6], [7, 8, 9]); for my $row (@AoA) { my ($sum, $count) = (0, 0); for my $col (@$row) { $sum += "$col "; $count ++; } print "Average: ", $sum / $count, "\n" if $count; }
Update: The comment about $a and $b was made earlier by zentara, but below in this thread, I had not noticed it when I mentioned that.Average: 2 Average: 5 Average: 8
In reply to Re^7: reading files in @ARGV doesn't return expected output
by Laurent_R
in thread reading files in @ARGV doesn't return expected output
by fasoli
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |