微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐