The well-known algorithm and code snippets in other languages can be found here:
http://stackoverflow.com/questions/9148543/program-to-print-permutations-of-given-elements
One python implementation is very brief and almost understandable (though my python knowledge is even poorer than my perl knowledge):
The biggest problem is (even after reading man perlsub for over an hour), I just don't understand the calling syntax. My attempt is looking something like this (which generates a huge number of syntax errors and won't run).def permute(in_str, prefix=""): if not len(in_str) : print(prefix) else: for i in range(0, len(in_str)): permute(in_str[:i] + in_str[i + 1:], prefix + in_str[i]) permute('12345')
#!/usr/bin/perl -w use strict; my @array = 1..5; while (permute @array) sub permute { my @temp; while ( $#temp < $#_) { my $i; for ($i = $#temp; $i < $#_; $i++) { swap @_[$i] @_[$#temp]; permute; swap @_[$i] @_[$#temp]; } } print $_; } sub swap { my $k; my $l; my $temp; $temp = $k; $k = $l; $l = $temp; }
Can someone guide me in the right direction. Clever techniques are not important. What is important is that I understand what is going on.
Thank you (very much) in advance.
In reply to generating permutations by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |