## Filtering tye's "combinations" (power set) iterator: my $iter = combinations(@S); while ( my @c = $iter->() ) { next unless @c == $K; ... }