1 use List::AllUtils qw/sum/; 2 use Bit::Vector; 3 4 my $k = 2; 5 6 my @letters = split '',"ABCDE"; 7 my $n = scalar(@letters); 8 9 my $vec = Bit::Vector->new($n); 10 11 while(!$vec->increment){ 12 my $i = $vec->to_Bin; # current bitstring 13 my $s = sum(split '',$i); # sum up to find out how many bits are lit 14 15 next unless ($s == $k); # just the ones with $k bits lit 16 17 18 my $str = # convert bitstring to string 19 join '', 20 map { $letters[$_] } 21 grep { $vec->contains($_); } 22 0..$n-1 ; 23 24 print "$s $i $str\n"; 25 };