http://www.perlmonks.org?node_id=434625


in reply to Re: Generating powerset with progressive ordering
in thread Generating powerset with progressive ordering

Huh - so apparently your method and my evil method ended up being the same, or at least very similar. Note that you can improve this slightly by consolidating your last two expressions and eliminating the need for an "e" flag on the substitutions:
for ($str) { s/0(0*)$/1$1/ or s/1(0*)1$/01$1/ or return; }
And actually, we can combine your method and the last bit in my post to get this somewhat natural structure for looping through the possibilities:
sub nextmask { if ($_[0] =~ s/0(0*)$/1$1/) {1;} elsif ($_[0] =~ s/1(0*)1$/01$1/) {2;} else {0;} } my @factors = qw(2 3 5 7); my $mask = '0' x scalar(@factors); while (my $transtype = nextmask($mask)) { print "BREAK\n" if ($transtype > 1); # do stuff print join " ", map { substr($mask, $_, 1) ? $factors[$_] : ' ' } 0 .. $#factors; print "\n"; }
(I've never liked do { ... } while() loops)
-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/