### Example Usage:
my $Text = "The quick brown fox jumps over the lazy dog";
my @Words = split(/ /, $Text);
printf("%s (%d words)\n", $Text, scalar @Words);
print "-"x100 . "\n";
foreach my $I (1..3) {
my @Results = Combinations($I, @Words);
printf("Found %d combinations of %d words from '%s'\n",
scalar @Results, $I, $Text);
printf(" (%s)\n", join("|", @$_) ) foreach (@Results);
print "-"x100 . "\n";
}
### Actual Useful Part:
sub Combinations {
my $PickSize = shift;
my @Elements = @_;
my @RValue;
my $StopAt = scalar(@Elements) - ($PickSize-1) - 1;
return ([]) if $PickSize == 0;
return ([]) if $PickSize > scalar(@Elements);
for(my $i=0; $i<=$StopAt; $i++) {
my $Element = $Elements[$i];
my @Rest = @Elements;
splice(@Rest, 0, $i+1);
foreach ( Combinations($PickSize-1, @Rest) ) {
my @NewElement = ($Element, @$_);
push(@RValue, \@NewElement );
}
}
return @RValue;
}
|