954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Process all possible permutations of a list

0
By KevinADC on Aug 28th, 2006 12:50 pm

"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";
}

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You