Create combinations from elements in an array
Asked Answered
A

4

7

I have an array reference like below:

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

I want to form all possible combination and create an array of array as:

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

What I tried:

foreach my $n(1..scalar(@array)) {
    my $iter = combinations($strings, $n);
    while (my $c = $iter->next) {
        print "@$c\n";
    }
}
Adriaadriaens answered 22/9, 2016 at 7:12 Comment(1)
Check this out: search.cpan.org/~allenday/Math-Combinatorics-0.09/lib/Math/…Rumba
M
2

Using Algorithm::Combinatorics to find all combinations.

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

Output:

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

Mussman answered 22/9, 2016 at 7:21 Comment(0)
S
2

If you don't have the module at hand, and you don't care about the outer level order:

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

Contents of @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"]]
Storeroom answered 22/9, 2016 at 11:15 Comment(0)
R
1

There's 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);

Output:

$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'
          ]
        ];
Rumba answered 22/9, 2016 at 7:55 Comment(0)
H
0

You can use the module "Algorithm::Combinatorics"

use Algorithm::Combinatorics "variations_with_repetition";
my @Variations = variations_with_repetition([qw(a b c d)], 4);
print "@$_\n", for @Variations;
Hath answered 22/9, 2016 at 7:31 Comment(1)
Output of your code is not same as requested by OP.Mussman

© 2022 - 2024 — McMap. All rights reserved.