in reply to Nested for loop: Add arrays values in 1 set of 10
Hello amitgsir, and welcome to the Monastery!
For calculating the sum of a list of numbers, the core module List::Util provides the sum function. And for iterating over an array in fixed-size chunks, the module List::MoreUtils provides natatime:
#! perl use strict; use warnings; use List::Util qw(sum); use List::MoreUtils qw(natatime); my @ints = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4); my $it = natatime 10, @ints; while (my @vals = $it->()) { my $sum = sum @vals; print $sum, "\n"; }
Output:
16:27 >perl 1338_SoPW.pl 10 40 30 40 16:27 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|