2016-09-22 2 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 # combination() – 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

Вы можете использовать модуль "Алгоритм :: комбинаторики"

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

Вывод вашего кода не совпадает с запросом OP. –

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 ([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

Если у вас нет модуля под рукой, и вы не заботитесь о порядке внешнего уровня:

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

@ yonyon100: Спасибо, что заметили это. Я исправил последнюю строку. –

Смежные вопросы