in reply to Sort of a permutation
this is a bit slower than the standard nested-loop approach, but it has the advantage that it is generic and you don't need to alter your code if you have more lists. (btw, I seem to remember merlyn having a similar, but much slicker permute function that does more or less the same thing lying around somewhere)sub permute{ my ($prefix,$c,@arrays)=@_; my @ret=(); foreach(@$c){ my $f=$prefix.$_; if(scalar(@arrays)==0){ push @ret,$f; }else{ push @ret,@{permute($f,@arrays)}; } } return \@ret; } my $perms=permute('',['C','A','T'],['D','O','G'],['B','I','R',D']);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jeffa) RE: Re: Sort of a permutation
by jeffa (Bishop) on Oct 19, 2000 at 08:22 UTC |