in reply to Permutation algorithm?
use Carp; sub print_possib { my @array = @_; my $pat = 0; # Counter used as a bit-vector print "(\n"; # A merlyn loop. { my $x = $pat; my @row; foreach my $i (@array) { if (2 == $i) { # Use the bit-vector to choose what to do now push @row, $x&1 ? 1 : 0; $x >>= 1; # Shift off the used bit } elsif (0 == $i or 1 == $i) { push @row, $i; } else { confess "Illegal value $i: Legal ones are 0, 1, and 0"; } } if ($x) { # We have more bits than needed, would start repeating. # So end instead print " )\n"; return; } else { print ( ($pat ? ",\n [ " : "( [ "), # Start or divider (join ", ", @row), # Row " ]" ); # Close the row ++$pat; redo; } } } print_possib(@ARGV);
|
|---|