And, of course, the 'Perl way' (such as it is) is not to worry about the values in the array or their number in the first place (what is it your business?), but just to use them:
>perl -wMstrict -le "my @values = (1, 2, 3, 4, 5); my $average; for my $value (@values) { $average += $value; } $average /= @values; print qq{average is $average}; " average is 3
List::Util::sum might also be useful in place of the explicit for-loop:
>perl -wMstrict -le "use List::Util qw(sum); my @values = (1, 2, 3, 4, 5); my $average = sum(@values) / @values; print qq{average is $average}; " average is 3
The next step might be to clean up that while-loop by only push-ing a value onto the array if it's a proper value (just get out of the loop if it isn't): no need for all those index-counting and flow-control variables.
In reply to Re: Use of uninitialized value in addition
by AnomalousMonk
in thread Use of uninitialized value in addition
by Solarplight
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |