我想在Perl中进行置换.例如我有三个数组:[“big”,“tiny”,“small”],然后我有[“red”,“yellow”,“green”]以及[“apple”,“pear” “.
如何得到:
["big","red","apple"] ["big","pear"] ..etc.. ["small","green","banana"]
我明白这被称为排列.但我不知道该怎么做.我也不知道我可以有多少个数组.可能有三四个,所以我不想做嵌套循环.
解决方法
这实际上不是排列,而是
Cartesian product.见
Math::Cartesian::Product.
#!/usr/bin/perl use strict; use warnings; use Math::Cartesian::Product; cartesian { print "@_\n" } ["big","tiny","small"],["red","yellow","green"],["apple","pear","banana"];
输出:
C:\Temp> uu big red apple big red pear big red banana big yellow apple big yellow pear big yellow banana big green apple big green pear big green banana tiny red apple tiny red pear tiny red banana tiny yellow apple tiny yellow pear tiny yellow banana tiny green apple tiny green pear tiny green banana small red apple small red pear small red banana small yellow apple small yellow pear small yellow banana small green apple small green pear small green banana