Here's a little program that generates all permutations of all the words on
each line of input. The algorithm embodied in the permut
function should work on any list:
#!/usr/bin/perl -n
# permute - tchrist@perl.com
permut([split], []);
sub permut {
my @head = @{ $_[0] };
my @tail = @{ $_[1] };
unless (@head) {
print "@tail\n";
} else {
my(@newhead,@newtail,$i);
foreach $i (0 .. $#head) {
@newhead = @head;
@newtail = @tail;
unshift(@newtail, splice(@newhead, $i, 1));
permut([@newhead], [@newtail]);
}
}
}