2016-09-22 20 views
7

の要素から組み合わせを作成しますように私はすべての可能な組み合わせを形成し、配列の配列を作成したい私は以下のような配列参照持つ配列

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"; 
    } 
} 
+1

これを確認してください:http://search.cpan.org/~ allenday/Math-Combinatorics-0.09/lib/Math/Combinatorics.pm#combine() – yonyon100

答えて

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' 
      ] 
     ]; 

0

あなたはMath::Combinatoricsがあります "アルゴリズム::組み合わせ論"

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

コードの出力は、OPによって要求されたものと同じではありません。 –

1

モジュールを使用することができます。

#!/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 ([email protected]); 
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' 
      ] 
     ]; 
2

あなたが手でモジュールを持っていない、とあなたは外側のレベルの順序を気にしない場合:@output

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

内容:

[["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"]] 
+1

@ yonyon100:ありがとうございました。私は最後の行を修正しました。 –

関連する問題