P is for Practical PerlMonks

### Comment on

 Need Help??

Your various examples and descriptions seem a bit inconsistent to me, so I am probably misunderstanding something. None the less, the following appears to do what your last example does, without the hard coded nested loops. It could easily be modified for different combinations of combinations and variations (ouch!)

update: replaced foreach loop in sub with a map to tidy it up.

```use strict;
use warnings;
use Algorithm::Combinatorics qw(combinations variations_with_repetitio
+n);

my @data = ('A', 'B', 'C', 'D', 'E', 'F');
my \$cardinality = 2;

for my \$cardinality (1..3) {

print "\n\nCardinality: \$cardinality\n";

my \$dataref = getPairsOfCombinations(\@data, \$cardinality);

if (\$dataref == 0) {
}

for (my \$i = 0; \$i < scalar(@\$dataref); \$i++) {

print "( " .  join( ',', map {  "( " . join(',', @{\$_}) . " )"
+  }   @{ \$dataref->[\$i] }  ) .  " )\n";

}
}

exit(0);

sub getPairsOfCombinations {
my (\$data, \$cardinality) = @_;

my @combinations = combinations(\@data, \$cardinality);
my @variations = variations_with_repetition([0..\$#combinations], 2
+);

return [ map { [ map { \$combinations[\$_] } @{\$_} ] } @variations ]
+;
}

This produces the following (abridged)

```
Cardinality: 1
( ( A ),( A ) )
( ( A ),( B ) )
( ( A ),( C ) )
( ( A ),( D ) )
( ( A ),( E ) )
( ( A ),( F ) )
( ( B ),( A ) )
( ( B ),( B ) )
( ( B ),( C ) )
( ( B ),( D ) )
( ( B ),( E ) )
( ( B ),( F ) )
( ( C ),( A ) )
( ( C ),( B ) )
( ( C ),( C ) )
( ( C ),( D ) )
( ( C ),( E ) )
( ( C ),( F ) )
( ( D ),( A ) )
( ( D ),( B ) )
( ( D ),( C ) )
( ( D ),( D ) )
( ( D ),( E ) )
( ( D ),( F ) )
( ( E ),( A ) )
( ( E ),( B ) )
( ( E ),( C ) )
( ( E ),( D ) )
( ( E ),( E ) )
( ( E ),( F ) )
( ( F ),( A ) )
( ( F ),( B ) )
( ( F ),( C ) )
( ( F ),( D ) )
( ( F ),( E ) )
( ( F ),( F ) )

Cardinality: 2
( ( A,B ),( A,B ) )
( ( A,B ),( A,C ) )
( ( A,B ),( A,D ) )
( ( A,B ),( A,E ) )
( ( A,B ),( A,F ) )
( ( A,B ),( B,C ) )
( ( A,B ),( B,D ) )
( ( A,B ),( B,E ) )
( ( A,B ),( B,F ) )
( ( A,B ),( C,D ) )
( ( A,B ),( C,E ) )
( ( A,B ),( C,F ) )
( ( A,B ),( D,E ) )
( ( A,B ),( D,F ) )
( ( A,B ),( E,F ) )
( ( A,C ),( A,B ) )
( ( A,C ),( A,C ) )
( ( A,C ),( A,D ) )
( ( A,C ),( A,E ) )
( ( A,C ),( A,F ) )
( ( A,C ),( B,C ) )
( ( A,C ),( B,D ) )
( ( A,C ),( B,E ) )
( ( A,C ),( B,F ) )
( ( A,C ),( C,D ) )
( ( A,C ),( C,E ) )
( ( A,C ),( C,F ) )
( ( A,C ),( D,E ) )
( ( A,C ),( D,F ) )
( ( A,C ),( E,F ) )
( ( A,D ),( A,B ) )
( ( A,D ),( A,C ) )
( ( A,D ),( A,D ) )
...

Cardinality: 3
( ( A,B,C ),( A,B,C ) )
( ( A,B,C ),( A,B,D ) )
( ( A,B,C ),( A,B,E ) )
( ( A,B,C ),( A,B,F ) )
( ( A,B,C ),( A,C,D ) )
( ( A,B,C ),( A,C,E ) )
( ( A,B,C ),( A,C,F ) )
( ( A,B,C ),( A,D,E ) )
( ( A,B,C ),( A,D,F ) )
( ( A,B,C ),( A,E,F ) )
( ( A,B,C ),( B,C,D ) )
( ( A,B,C ),( B,C,E ) )
( ( A,B,C ),( B,C,F ) )
( ( A,B,C ),( B,D,E ) )
( ( A,B,C ),( B,D,F ) )
( ( A,B,C ),( B,E,F ) )
( ( A,B,C ),( C,D,E ) )
( ( A,B,C ),( C,D,F ) )
( ( A,B,C ),( C,E,F ) )
( ( A,B,C ),( D,E,F ) )
( ( A,B,D ),( A,B,C ) )
( ( A,B,D ),( A,B,D ) )
( ( A,B,D ),( A,B,E ) )
( ( A,B,D ),( A,B,F ) )
( ( A,B,D ),( A,C,D ) )
( ( A,B,D ),( A,C,E ) )
( ( A,B,D ),( A,C,F ) )
( ( A,B,D ),( A,D,E ) )
( ( A,B,D ),( A,D,F ) )
( ( A,B,D ),( A,E,F ) )
( ( A,B,D ),( B,C,D ) )
( ( A,B,D ),( B,C,E ) )
( ( A,B,D ),( B,C,F ) )
( ( A,B,D ),( B,D,E ) )
( ( A,B,D ),( B,D,F ) )
( ( A,B,D ),( B,E,F ) )
( ( A,B,D ),( C,D,E ) )
( ( A,B,D ),( C,D,F ) )
( ( A,B,D ),( C,E,F ) )
( ( A,B,D ),( D,E,F ) )
( ( A,B,E ),( A,B,C ) )
( ( A,B,E ),( A,B,D ) )
( ( A,B,E ),( A,B,E ) )
( ( A,B,E ),( A,B,F ) )
( ( A,B,E ),( A,C,D ) )
( ( A,B,E ),( A,C,E ) )
( ( A,B,E ),( A,C,F ) )
( ( A,B,E ),( A,D,E ) )
( ( A,B,E ),( A,D,F ) )
( ( A,B,E ),( A,E,F ) )
( ( A,B,E ),( B,C,D ) )
( ( A,B,E ),( B,C,E ) )
( ( A,B,E ),( B,C,F ) )
( ( A,B,E ),( B,D,E ) )
( ( A,B,E ),( B,D,F ) )
( ( A,B,E ),( B,E,F ) )
( ( A,B,E ),( C,D,E ) )
( ( A,B,E ),( C,D,F ) )
( ( A,B,E ),( C,E,F ) )
( ( A,B,E ),( D,E,F ) )
( ( A,B,F ),( A,B,C ) )
( ( A,B,F ),( A,B,D ) )
( ( A,B,F ),( A,B,E ) )
( ( A,B,F ),( A,B,F ) )
( ( A,B,F ),( A,C,D ) )
( ( A,B,F ),( A,C,E ) )
( ( A,B,F ),( A,C,F ) )
( ( A,B,F ),( A,D,E ) )
( ( A,B,F ),( A,D,F ) )
( ( A,B,F ),( A,E,F ) )
( ( A,B,F ),( B,C,D ) )
( ( A,B,F ),( B,C,E ) )
( ( A,B,F ),( B,C,F ) )
( ( A,B,F ),( B,D,E ) )
( ( A,B,F ),( B,D,F ) )
( ( A,B,F ),( B,E,F ) )
( ( A,B,F ),( C,D,E ) )
( ( A,B,F ),( C,D,F ) )
( ( A,B,F ),( C,E,F ) )
( ( A,B,F ),( D,E,F ) )
...

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [jedikaiti]: Hi, Monks [marto]: heya jedikaiti! [jedikaiti]: How's life, marto? [marto]: I saw this robot the other day, pretty fancy stuff. Things are pretty good jedikaiti, improving :) How are things with you? [jedikaiti]: Doing OK, but a little achy this AM - I think the cats were using me as a wrestling mat and mattress while I was asleep last night.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (17)
As of 2017-07-25 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (374 votes). Check out past polls.