You can write loops, or you can use modules.
Baidu for a long time to find a blog http://blog.sina.com.cn/s/blog_4a0824490101f1kc.html detailed introduction of Algorithm::combinatorics
Inspired by this, I found math::combinatorics.
Because the previous blog introduced algorithm::combinatorics, so this blog about Math::combinatorics
Perl script
Use Math::combinatorics;
My @n = QW (a b c);
My $combinat = math::combinatorics->new (count = 2,data = [@n]);
Print "Combinations of 2 from:". Join ("", @n). " \ n ";
Print "------------------------". ("--" x scalar (@n)). " \ n ";
while (my @combo = $combinat->next_combination) {
Print join (", @combo)." \ n ";
}
print "\ n";
Print "Display the permutations:". Join ("", @n). " \ n ";
Print "------------------------". ("--" x scalar (@n)). " \ n ";
while (my @permu = $combinat->next_permutation) {
Print join (", @permu)." \ n ";
}
Results
Combinations of 2 from:a b C
------------------------------
A b
A C
b C
Display the Permutations:a b C
------------------------------
A b C
A C b
b A C
b c A
c A B
C B A
Get all the combinatorial problems of k elements in an array of n elements