Skip to content

Instantly share code, notes, and snippets.

@davestevens
Created June 24, 2012 14:07
Show Gist options
  • Save davestevens/2983346 to your computer and use it in GitHub Desktop.
Save davestevens/2983346 to your computer and use it in GitHub Desktop.
Attempt to display all combinations of data set
#!/usr/bin/perl
use strict;
use Data::Dumper;
my @b = (['a','b'],['c','d'],['e','f']);
my @all;
my @single;
&rec(0, \@all, \@single, @b);
print Dumper(@all);
# pass depth, reference to return array, reference to single storage array and data to recurse over
sub rec {
my @data = @_;
my $d = shift(@data);
my $aP = shift(@data);
my $sP = shift(@data);
for(my $i=0;$i<@{$data[$d]};$i++) {
$$sP[$d] = $data[$d][$i];
if($d == $#data) {
push(@$aP, [@$sP]);
}
else {
&rec(($d+1), $aP, $sP, @data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment