http://www.perlmonks.org?node_id=191530


in reply to How can I add all the numbers in an array with out doing a foreach loop?

Another recursive method:

@array = (1,2,3); print "SUM: ", sum($#array); sub sum { if ($_[0] == 0) { return $array[0]; } return $array[$_[0]] + sum($_[0]-1); } #note it's not efficient

This method's shortcoming is that the sub acts only on the global array "@array", and thus can't be easily applied to more than one array. A more elegant and useable recursive method was demonstrated earlier in this thread.

Originally posted as a Categorized Answer.