in reply to unordered sets of N elements
If you want an iterator (rather than a recursive function that returns all relevant combinations), that is easy too: it's just like counting in base n, except that when you overflow you reset the overflowing digit not to zero but to the same (new) value as the preceding digit:
sub next_iter { my($count, $die, $current) = @_; return '1' x $count unless $current; $current =~ s{ (?!$die) ((.) $die*) $ }{ my $d = $2 + 1; $d x length $2 }ex or return undef; return $current; } ... my $cur; print $cur while $cur = next_iter(3, 6, $cur);
Presenting the strings reversed instead allows a slightly cleaner solution with a (rare) useful use of 'cut' - just change the pattern to:
.. and the cut takes up the role of the negative lookahead in ensuring that our incremented digit cannot itself be a match for $die.( (?>$die*) (.))
Hugo
|
---|