从数组中的元素创建组合

时间:2016-09-22 07:12:39

标签: arrays perl perl-module perl-data-structures

我有一个如下所示的数组引用:

my $strings = [qw(a b c d)];

我想形成所有可能的组合并创建一个数组数组:

my $output = [qw(qw([a],[b],[c],[d],[a,b],[a,c],[a,d],[b,c],[b,d],[c,d],   [a,b,c],[a,b,d],[b,c,d],[a,b,c,d]))]

我尝试了什么:

foreach my $n(1..scalar(@array)) {
    my $iter = combinations($strings, $n);
    while (my $c = $iter->next) {
        print "@$c\n";
    }
}

4 个答案:

答案 0 :(得分:2)

使用Algorithm::Combinatorics查找所有组合。

#!/#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Algorithm::Combinatorics qw(combinations);
my @data = qw(a b c d);
my $all_combinations;
foreach (1..4){
    push @$all_combinations, combinations(\@data, $_);
}
print Dumper $all_combinations;

输出:

$VAR1 = [
          [
            'a'
          ],
          [
            'b'
          ],
          [
            'c'
          ],
          [
            'd'
          ],
          [
            'a',
            'b'
          ],
          [
            'a',
            'c'
          ],
          [
            'a',
            'd'
          ],
          [
            'b',
            'c'
          ],
          [
            'b',
            'd'
          ],
          [
            'c',
            'd'
          ],
          [
            'a',
            'b',
            'c'
          ],
          [
            'a',
            'b',
            'd'
          ],
          [
            'a',
            'c',
            'd'
          ],
          [
            'b',
            'c',
            'd'
          ],
          [
            'a',
            'b',
            'c',
            'd'
          ]
        ];

答案 1 :(得分:2)

如果您手边没有模块,并且您不关心外层级别订单:

sub fu { 
  my ($base,@rest) = @_;
  my @result = @$base && $base || ();
  push @result, fu( [@$base, shift @rest], @rest) while @rest;
  return @result;
}
my @output = fu([],qw(a b c d));

@output的内容:

[["a"],["a","b"],["a","b","c"],["a","b","c","d"],["a","b","d"],["a","c"],["a","c","d"],["a","d"],["b"],["b","c"],["b","c","d"],["b","d"],["c"],["c","d"],["d"]]

答案 2 :(得分:1)

Math::Combinatorics

#!/usr/bin/perl
use strict;
use warnings;
use Math::Combinatorics qw(combine);
use Data::Dumper;

my @n = qw(a b c d);
my @res;
push @res, combine($_, @n) foreach (0..@n);
print Dumper(\@res);

输出:

$VAR1 = [
          [
            'b'
          ],
          [
            'c'
          ],
          [
            'a'
          ],
          [
            'd'
          ],
          [
            'c',
            'a'
          ],
          [
            'c',
            'd'
          ],
          [
            'c',
            'b'
          ],
          [
            'a',
            'd'
          ],
          [
            'a',
            'b'
          ],
          [
            'd',
            'b'
          ],
          [
            'b',
            'a',
            'd'
          ],
          [
            'b',
            'a',
            'c'
          ],
          [
            'b',
            'd',
            'c'
          ],
          [
            'a',
            'd',
            'c'
          ],
          [
            'b',
            'c',
            'd',
            'a'
          ]
        ];

答案 3 :(得分:0)

您可以使用模块" Algorithm :: Combinatorics"

use Algorithm::Combinatorics "variations_with_repetition";
my @Variations = variations_with_repetition([qw(a b c d)], 4);
print "@$_\n", for @Variations;