in reply to How can I add all the numbers in an array with out doing a foreach loop?
Or, equivalently, use the map function:my $sum; grep { $sum += $_ } @arrayToSum;
Both grep and map apply the code in the code block to each element of @arrayToSum. The _result_ of the grep/map function is not interesting to us in this case, so we ignore it.my $sum; map { $sum += $_ } @arrayToSum;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Answer: How can I add all the numbers in an array with out doing a foreach loop?
by MidLifeXis (Monsignor) on Oct 21, 2013 at 13:03 UTC | |
|
Re: Answer: How can I add all the numbers in an array with out doing a foreach loop?
by Eily (Monsignor) on Oct 22, 2013 at 07:44 UTC |