Generating all the combinations then filtering out the unneeded ones could take a lot lot lot longer than necessary. This is what the earlier solutions do. However, the following program avoids generating extra combinations.

use strict; use warnings; use feature qw( say ); use Algorithm::Loops qw( NestedLoops ); use Math::Combinatorics qw( combine ); use Sort::Key qw( rukeysort ); # Optional. my @strings = qw( a?1 a?2 a%3 b?2 b?3 b%5 c%4 c%5 c?6 d%2 ); my @mandatory_types = qw( a? b% c? ); my $n = 4; $n -= @mandatory_types; # ( [ "a?1", "a?", 1 ], ... ) my @split_strings = map { [ /^((.+)(\d+))\z/a ] } @strings; my %by_weight; for ( @split_strings ) { push @{ $by_weight{ $_->[2] } }, $_; } my @mandatory_lists = map { my $type = $_; [ grep { $_->[1] eq $type } @split_strings ] } @mandatory_types; my @loops; for my $list ( @mandatory_lists ) { push @loops, sub { my %seen_weights = map { $_->[2] => 1 } @_; return [ grep { !$seen_weights{ $_->[2] } } @$list ]; }; } my $mandatory_iter = NestedLoops( \@loops ); while ( my @mandatory_picks = $mandatory_iter->() ) { my %seen_weights = map { $_->[2] => 1 } @mandatory_picks; my @buckets = map { $by_weight{ $_ } } grep { !$seen_weights{ $_ } } keys( %by_weight ); my $bucket_iter = Math::Combinatorics->new( count => $n, data => \@buckets, ); while ( my @bucket_picks = $bucket_iter->next_combination() ) { my $other_iter = NestedLoops( \@bucket_picks ); while ( my @other_picks = $other_iter->() ) { say join ",", map { $_->[0] } rukeysort { $_->[2] } @mandatory_picks, @other_picks; } } }
c?6,b%5,a?2,a?1 c?6,b%5,b?2,a?1 c?6,b%5,d%2,a?1 c?6,b%5,a%3,a?1 c?6,b%5,b?3,a?1 c?6,b%5,c%4,a?1 c?6,b%5,a%3,a?2 c?6,b%5,b?3,a?2 c?6,b%5,c%4,a?2 c?6,b%5,a?2,a?1

More interesting test:

my @strings = qw( a?1 a?2 a%3 b?2 b?3 b%5 c%4 c%5 c?6 d%2 a?7 c?7 ); my @mandatory_types = qw( a? b% c? ); # ^^^^^^^ my $n = 5; # ^
a?7,c?6,b%5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,a?2 c?7,c?6,b%5,b?3,a?2 c?7,b%5,a%3,a?2,a?1 c?7,b%5,b?3,a?2,a?1 c?7,b%5,c%4,a?2,a?1 c?7,c?6,b%5,a?2,a?1 c?7,b%5,c%4,a%3,a?2 c?7,b%5,c%4,b?3,a?2 c?7,c?6,b%5,a%3,a?2 c?7,c?6,b%5,b?3,a?2 c?7,c?6,b%5,c%4,a?2 a?7,c?6,b%5,c%4,a?2 a?7,c?6,b%5,c%4,b?2 a?7,c?6,b%5,c%4,d%2 a?7,c?6,b%5,a%3,a?2 a?7,c?6,b%5,b?3,a?2 a?7,c?6,b%5,a%3,b?2 a?7,c?6,b%5,b?3,b?2 a?7,c?6,b%5,a%3,d%2 a?7,c?6,b%5,b?3,d%2 a?7,c?6,b%5,a?2,a?1 a?7,c?6,b%5,b?2,a?1 a?7,c?6,b%5,d%2,a?1 a?7,c?6,b%5,c%4,a%3 a?7,c?6,b%5,c%4,b?3 a?7,c?6,b%5,c%4,a?1 a?7,c?6,b%5,a%3,a?1 a?7,c?6,b%5,b?3,a?1

In reply to Re: Combinations with constraints by ikegami
in thread Combinations with constraints by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.