in reply to How do I get all n element subsets of an array

my @x = qw(a b c d e f); my $n = 4; my $x = (1<<$n)-1; while( $x < 1<<@x ){ my $i = $x<<1; print join", ",(grep{($i>>=1)&1}@x),"\n"; $i = $x&(~$x>>1); $i &= -$i; $x += $i--; if( $i &= $x ){ $x -= $i; $i /= ($i&-$i); $x += $i; } }