use strict; my @array = ["a","b","c","d"]; my $newlist = combine(2,@array); foreach my $list (@$newlist) { print "[" . join(",", @$list) . "]\n"; } # takes a list of items # returns a list of lists of those items for each combination sub combine { my $length = shift; my $items = shift; my @list; if ($length == 1) { foreach my $item (@$items) { push @list,[$item]; } } else { my $l = length(@$items) + 1; foreach (0..$l) # once for each item { my $tempa = shift @$items; # get first item(a) # get permutations for this set, but one # shorter (without current item) my $templist = combine($length-1,$items); foreach my $i (@$templist) { unshift @$i,$tempa; push @list,$i; } push @$items,$tempa; } } return [@list]; } 1;