in reply to adding combinations of arrays (of arrays)

It does what you're trying to do, only with single values rather than arrays. I think I know what you're trying to do now, though I have no idea why:

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

You can change the number of elements in the arrays or AoA's and also the value for $n and you should get accurate results regardless.

Replies are listed 'Best First'.
Re^2: adding combinations of arrays (of arrays)
by aquinom (Sexton) on Nov 30, 2011 at 00:36 UTC
    Hehe well the reason is that these integer values are numbers of non-reference alleles at specific sites and and AoA will be created for each gene and I'm taking these combinations and doing a correlation on them to a case-control pattern so to see if adding any of the sites in a gene produces a fit to that model. Yeah it's all hypothetical so if you're a statistician I know this likely isn't an optimal approach to looking for mutations at many sites over a gene to identify a target gene for further testing...yeah I am sure you didn't want to know all that much anyways!
Re^2: adding combinations of arrays (of arrays)
by Anonymous Monk on Nov 30, 2011 at 01:17 UTC
    Thanks, some preliminary tests on this work so I think you nailed it. I'm very grateful.