I have about 50 arrays filled with numbers and I have to report out a sum of all those elements for every array so the end result has 50 sums of individual arrays.
Is there any shorter way to do it rather than writing different for loop for every array?
I am a beginner with perl. Any helpful comments/suggestions would be appreciated.
This is my code so far:
for( $j = 1 ; $j <= 50 ; $j++ ) {
for ( @arr[$j] ) {
$sum[$j] += $_;
}
print $sum[$j];
}
Thank you!
@arr(index$jis 1) and ending at the fifty-first element (index 50). Perl's more expressive loops —for my $whatever (@whatevers)— avoid this kind of error, as doeswhile (my ($index, $element) = each(@array))available in 5.12 and later.