Process all possible permutations of a list

KevinADC 0 Tallied Votes 170 Views Share

"How can I find all the permutations of a list (or a word)"? I have seen this question asked a number of times on forums. There is a module that makes this task very easy: List::Permutor. It is not a core module so you may need to install it. See module for more details: List::Permutor

# permutations of an array:

use List::Permutor;
my @array = qw(Mary had a little lamb);
my $perm = new List::Permutor @array;
while (my @set = $perm->next) {
   print "One order is @set.\n";
} 


# permutations of a word:

use List::Permutor;
my $word = 'perl';
my $perm = new List::Permutor split(//,$word);
while (my @set = $perm->next) {
   print "One order is @set.\n";
}