in reply to Nested for loop: Add arrays values in 1 set of 10

Thank you very much Athanasius for your warm welcome and Kind reply. It really helped. Looks like I need to step-up to use the CPAN-Modules. Thanks again!! Amit
  • Comment on Re: Nested for loop: Add arrays values in 1 set of 10

Replies are listed 'Best First'.
Re^2: Nested for loop: Add arrays values in 1 set of 10
by Laurent_R (Canon) on Aug 12, 2015 at 08:43 UTC
    The List::Utils module is core, meaning that it is delivered and installed with any distribution of Perl. The List::MoreUtils probably needs to be installed from the CPAN.

    And, yes, you should probably use them, but just in case you worry about how to do it in "pure Perl" (i.e. not using modules), which might be useful for learning purposes, this small session under the Perl debugger gives an idea on how you might do it:

    DB<1> @array = qw/1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 + 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3/; DB<2> while (@array) { $sum = 0; $sum += shift @array for 1..10; pri +nt "$sum\n";}; 10 40 20 30
    A real life program would need to do a bit more error checking than that, but you've got the basic idea. Note that at the end, the array is empty, so that if you need to keep the array you would have to first do a copy of it.