EpiMan
EpiMan

Reputation: 839

how to save combinations generated by (Algorithm::Combinatorics) in an array of array

I have an array (@strings) including some words. I want to generate all possible combinations and save the results in a data structure like array of array. I found a post "In Perl, how can I generate all possible combinations of a list?" but I could not save it in an array of array

#!/usr/bin/perl
use strict;
use warnings;
use Algorithm::Combinatorics qw(combinations);

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

AAA BBB CCC DDD EEE

my $iter = combinations(\@strings, 2);
while (my $c = $iter->next) {
    print "@$c\n";
}

I tried:

my @a;
while (my $c = $iter->next) {
    push @a, @$c;
    }

Upvotes: 0

Views: 170

Answers (1)

ikegami
ikegami

Reputation: 385655

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");
}

Upvotes: 2

Related Questions