in reply to Re: Nested for loop: Add arrays values in 1 set of 10
in thread Nested for loop: Add arrays values in 1 set of 10
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:
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.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
|
|---|