2016-09-22 7 views
7

मैं नीचे की तरह एक सरणी संदर्भ में तत्वों से संयोजन तैयार करें:एक सरणी

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

मैं क्या करने की कोशिश की:

my $strings = [qw(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 # गठबंधन() – 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

आपके कोड का आउटपुट ओपी द्वारा अनुरोध के समान नहीं है। –

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

आप हाथ में मॉड्यूल की जरूरत नहीं है, और आप बाहरी स्तर आदेश के बारे में परवाह नहीं है: @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: इसे ध्यान में रखने के लिए धन्यवाद। मैंने आखिरी पंक्ति को सही किया है। –