2016-09-22 17 views
7

mam odniesienie array jak poniżej:Twórz kombinacje z elementów w tablicy

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

Chcę utworzyć wszystkie możliwe kombinacje i utworzyć tablicę tablicy jako:

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

co starałem :

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

Sprawdź to: http://search.cpan.org/~ allenday/Math-Combinatorics-0.09/lib/Math/Combinatorics.pm # combine() – yonyon100

Odpowiedz

2

Korzystanie z Algorithm::Combinatorics, aby znaleźć wszystkie kombinacje.

#!/#!/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; 

wyjściowa:

$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

można użyć modułu "algorytm :: kombinatoryki"

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

Wynik twojego kodu jest inny niż żądany przez OP. –

1

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

wyjściowa:

$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

Jeśli nie masz modułu pod ręką, i nie dbam o porządek na poziomie zewnętrznym:

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

zawartość @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: Dziękuję za uwagę. Poprawiłem ostatnią linię. –