in reply to How to show all possible replacements?
Instead of glob you can also use explicit enumeration of all possibilities. There are 2**n of them if n is the length of your word. If you translate all numbers from 0 to 2**n-1 into binary and translate 0 into the empty string, you get all combinations. The following script compares run time against glob.
use strict; use warnings; use Time::HiRes qw[ time ]; # explicit enumeration my $s = time; my $string = "aabc"; my $n = length $string; my $p = 2**$n; for( my $i=0; $i<$p; $i++ ) { my $x = sprintf( "%0${n}b", $i ); print substr( $string, $_, 1 ), substr( $x, $_, 1) ? "1" : "" for 0. +.$n-1; print "\n"; } print STDERR "time: ", time-$s, "\n"; # using glob $s = time; my $g = ""; $g .= substr( $string, $_, 1 ).'{,1}' for 0..$n-1; print "$_\n" while( glob $g ); print STDERR "time: ", time-$s, "\n";
On my machine it seems that explicit enumeration is slightly faster but not much.
|
|---|