Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) 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) { die "Bad return\n"; } 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 ) ) ...

In reply to Re: Generalizing Code: Generating Unique Permutations by ig
in thread Generalizing Code: Generating Unique Permutations 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!
  • 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, 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
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2014-12-26 21:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (176 votes), past polls