2011-02-09 83 views
0

我想枚举元素数组的所有可能的组合。例如:我有数组:如何枚举元素的所有可能组合

$r = ('a1','a2','a3' ...). 

我想打印元件阵列的所有组合: A1A2,a1a3,A1A2A3等。 其中 A1A2 = a2a1,A1A2A3 = a1a3a2 ...

回答

4

事实证明,有a module为:!

use Math::Combinatorics; 

@r = qw(a1 a2 a3 ...); 
#@all_combinations_of_r = map { combine($_,@r) } [email protected]; 
@all_permutations_of_r = map { permute(@$_) } map { combine($_,@r) } [email protected]; 
+1

从例如它,而看的是`permute`会更适合。最后一项显示订单或项目很重要。 – bvr 2011-02-09 16:06:04