in reply to Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's
Fun with regex :)
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11121888 use warnings; print "$_\n" for strings( 4, 10 ); sub strings { my ($ones, $length) = @_; my @strings; my $pattern = '(1?)' x $length; (1 x $ones) =~ /^$pattern$(?{ push @strings, join '', map $_ || 0, @{^CAPTURE}; })(*FAIL)/x; return @strings; }
UPDATE: removed a useless join
In Section
Seekers of Perl Wisdom