#!perl -w
use strict;
# kenhirsch at myself.com 2002-08-23
my $r = shift or die "usage: combinations r a b c d e ...\n";
my @out = combinations($r, \@ARGV);
for (@out) {
print join(" ", @{$_}), "\n";
}
# From Algorith L in Knuth Vol. 4 Sec 7.2.1.3 (not yet published)
sub combinations {
my ($t, $arrayref) = @_;
my @c = 0 .. $t-1;
my @range = reverse @c;
my $j;
my @result;
$c[$t] = scalar(@{$arrayref});
$c[$t + 1] = 0;
do {
push @result, [@{$arrayref}[@c[@range]]];
for ($j=0; $c[$j] + 1 == $c[$j+1]; $j++) {
$c[$j] = $j;
}
$c[$j]++;
} while ($j < $t);
return @result;
}