首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将(Algorithm::Combinatorics)生成的组合保存到数组的数组中

如何将(Algorithm::Combinatorics)生成的组合保存到数组的数组中
EN

Stack Overflow用户
提问于 2015-03-12 04:44:37
回答 1查看 133关注 0票数 0

我有一个包含一些单词的数组(@string)。我想生成所有可能的组合,并将结果保存在一个类似array of array的数据结构中。我找到一个帖子"In Perl, how can I generate all possible combinations of a list?“,但我不能将它保存在数组的数组中

代码语言:javascript
复制
#!/usr/bin/perl
use strict;
use warnings;
use Algorithm::Combinatorics qw(combinations);

print join("\t", @strings),"\n";

AAA BBB CCC DDD EEE

代码语言:javascript
复制
my $iter = combinations(\@strings, 2);
while (my $c = $iter->next) {
    print "@$c\n";
}

我试过了:

代码语言:javascript
复制
my @a;
while (my $c = $iter->next) {
    push @a, @$c;
    }
EN

回答 1

Stack Overflow用户

发布于 2015-03-12 06:07:04

代码语言:javascript
复制
use Algorithm::Combinatorics qw( combinations );
use Parallel::ForkManager    qw( );

use constant MAX_WORKERS => 10;

my @strings = ...;

my $pm = Parallel::ForkManager->new(MAX_WORKERS);
my $iter = combinations(\@strings, 2);

while (my $c = $iter->next) {
   my $pid = $pm->start and next;

   exec('program', @$c)
      or die("Can't execute child: $!\n");
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28996978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档