在Perl中,如何获得多套笛卡儿积分?

前端之家收集整理的这篇文章主要介绍了在Perl中,如何获得多套笛卡儿积分?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在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

猜你在找的Perl相关文章