#!/usr/bin/perl -w use strict; #how many options should be presented at minimum? my $minpermutelength = $ARGV[0]; #this will be set to 0..$#array_passed_in when all is working my @slots = (0..4); my $biggestslotnum = $#slots; die("Minimum permute length $minpermutelength is larger than the data set of 0..$#slots\n") if ($minpermutelength > $#slots); die("Minimum permute length cannot be negative or undefined.\n") if ($minpermutelength < 0 || !defined $minpermutelength); my $loop = 0; my $iteration = 0; my $thispermute = $#slots; while ($#slots >= $minpermutelength) { while ($slots[$#slots] <= $biggestslotnum) { for (@slots) { print "$_" }; print " loop: $loop iteration: $iteration thispermute: $thispermute\n"; # record $slots[$#slots]++; } $slots[$#slots]--; # fix overage from while above if ($slots[$#slots] == $biggestslotnum && $slots[0] == $biggestslotnum - $#slots && $loop == 0) { $thispermute--; # shrink permute string by one @slots = (0..$biggestslotnum); # reset splice(@slots,$thispermute-$biggestslotnum); # 0 1 2 3 $iteration = 0; next; } if ($slots[$#slots-$iteration-1] == $slots[$#slots-$iteration]-1) { # 0 3 4 $loop = 0; $iteration++; $slots[$#slots-$iteration-1]++; # I think my problem is in these splices, but I can't figure it out. :( if ($slots[$#slots-$iteration] - $slots[$#slots-$iteration-1] > 1) { splice(@slots,$slots[$#slots-$iteration-1],$thispermute); splice(@slots,@slots,0,$slots[$#slots-$iteration-1]+1..(($slots[$#slots-$iteration-1]+1)+($thispermute-@slots))); $slots[$#slots]++ while ($slots[$#slots] <= $slots[$#slots-1]); $iteration = 0; } } else { $loop++; $slots[$#slots-1]++; #increase the slot to the left by one $slots[$#slots] = $thispermute + $loop unless ($slots[$#slots-1] == $slots[$#slots]-1); # start counting again $slots[$#slots]++ while ($slots[$#slots] <= $slots[$#slots-1]); } }