in reply to adding combinations of arrays (of arrays)

Not sure precisely what you're trying to do, since your code is one big mass of logic errors and bad Perl. But here's some code for NCN. I use recursion to allow for the variable number of nested loops.

use strict; use warnings; my @vals = (1, 2, 3, 4, 5); my $n = 3; my @solutions; nSum(\@vals, \@solutions, 0, -1, $n); print "@solutions\n"; sub nSum { my ($vals, $solutions, $sum, $i, $n) = @_; if (--$n) { nSum($vals, $solutions, $sum + $vals->[$_], $_, $n) for ($i+1)..$#$vals; } else { push @$solutions, $sum + $vals->[$_] for ($i+1)..$#$vals; } }

Replies are listed 'Best First'.
Re^2: adding combinations of arrays (of arrays)
by aquinom (Sexton) on Nov 29, 2011 at 22:36 UTC
    Not sure what your code is supposed to do, it prints: 6 7 8 8 9 10 9 10 11 12. But anyways, you should see more clearly what my idea is now if you run my updated code.