# perl多重循环生成全排列精简

AAAAAAAAAAAA

AAAAAAAAAAAT

AAAAAAAAAAAC

AAAAAAAAAAAG

AAAAAAAAAATA

`````````````````````````

perl -le '{\$a{"00"}="A";\$a{"01"}="T";\$a{"10"}="C";\$a{"11"}="G";for(0..100){print join"",map{\$a{\$_}} unpack("a2"x12,sprintf("%024b\n",\$_))}}'

AAAAAAAAAAAA

AAAAAAAAAAAT

AAAAAAAAAAAC

AAAAAAAAAAAG

AAAAAAAAAATA

AAAAAAAAAATT

AAAAAAAAAATC

AAAAAAAAAATG

AAAAAAAAAACA

AAAAAAAAAACT

AAAAAAAAAACC

AAAAAAAAAACG

AAAAAAAAAAGA

AAAAAAAAAAGT

AAAAAAAAAAGC

AAAAAAAAAAGG

AAAAAAAAATAA

AAAAAAAAATAT

perl -le '{\$a=sprintf("%024b\n",1);print \$a}'

000000000000000000000001

perl -le '{@a=unpack("a2"x12,sprintf("%024b\n",100));print join":", @a}'

00:00:00:00:00:00:00:00:01:10:01:00

unpack这个函数很简单，就是把二进制的数值拆分成字符串数组，两个数字组成一个字符串。

[perl]

#!/usr/bin/perl

my \$arr = [];

\$arr->[\$_] = 0 for (0..11);
my @b = ("A","C","G","T");

while(1){
print join "", map {\$b[\$_]} @\$arr;
print "\n";
exit unless &count(\$arr,11);
}

sub count {
my \$arr = shift;
my \$x = shift;

return 0 if \$x < 0;

if (\$arr->[\$x] < 3){
\$arr->[\$x]++;
return 1;
}

else{
\$arr->[\$x] = 0;
return &count(\$arr,\$x - 1);
}
}

[/perl]

[perl]

#!/usr/bin/perl -w

my @A = qw(A T G C);
my \$N = 12;

foreach my \$n (0..4**\$N){
foreach my \$index (0..\$N){
print \$A[\$n%4];
\$n = \$n >>2;
}
print "\n";
}

[/perl]