W
weberh
Hi !
All scripts for permutation I found yet, were restricted by depth
(loops have to be hardcoded) or memory (recursive approach).
Here I wrote a handy script that gives the user unrestricted
choice of number of elements and depth of permutation.
The script is a bit special, because it dynamically generates code.
This code delivers the permutation and is evaluated within the script.
So, "perm 4 red green blue" gives
redredredred
redredredgreen
..
..
blueblueblueblue
It's still an ad hoc script and has to be refined, of course.
Would you like to see it as part of a module ?
Maybe together with other flavors of permutation and combination ?
Here we go ...
#!/usr/bin/perl
#perm: permutation with user defined depth and number of elements
#usage: perm depth element1 element2 ...
use strict;
my ($code,$i);
my ($depth,@element) = @ARGV;
for ($i = 1; $i <= $depth; $i++) #Code generation starts here
{$code .= "for (my \$i$i = 0; \$i$i <= $#element; \$i$i++)\n{"}
$code .= 'print "';
for (my $c=1; $c <= $depth; $c++)
{$code .= "\$element[\$i$c]"}
$code .= '\n"';
for (my $c=1; $c <= $i-1; $c++)
{$code .= '}'}
$code .= "\n"; #Code generation is finished here
eval($code); #Code evaluation gives the permutation
Regards, Harald
All scripts for permutation I found yet, were restricted by depth
(loops have to be hardcoded) or memory (recursive approach).
Here I wrote a handy script that gives the user unrestricted
choice of number of elements and depth of permutation.
The script is a bit special, because it dynamically generates code.
This code delivers the permutation and is evaluated within the script.
So, "perm 4 red green blue" gives
redredredred
redredredgreen
..
..
blueblueblueblue
It's still an ad hoc script and has to be refined, of course.
Would you like to see it as part of a module ?
Maybe together with other flavors of permutation and combination ?
Here we go ...
#!/usr/bin/perl
#perm: permutation with user defined depth and number of elements
#usage: perm depth element1 element2 ...
use strict;
my ($code,$i);
my ($depth,@element) = @ARGV;
for ($i = 1; $i <= $depth; $i++) #Code generation starts here
{$code .= "for (my \$i$i = 0; \$i$i <= $#element; \$i$i++)\n{"}
$code .= 'print "';
for (my $c=1; $c <= $depth; $c++)
{$code .= "\$element[\$i$c]"}
$code .= '\n"';
for (my $c=1; $c <= $i-1; $c++)
{$code .= '}'}
$code .= "\n"; #Code generation is finished here
eval($code); #Code evaluation gives the permutation
Regards, Harald