We don't bite newbies here... much PerlMonks

### Comment on

 Need Help??

Howdy all,

I have a function that I use sometimes when I need permutations (I do permutation-testing often for stats problems). Currently I require the user to pass the "cardinality": how many different elements to put in each class. Let me give a simple example first, before I show the code:

```Data: a1, b2, c3

Cardinality: 1
Permutations:
{a1, b2},
{a1, c3},
{b2, c3},

Data: a1, b2, c3, d4
Cardinality: 2
Permutations:
{{a1, b2}, {c3, d4}}
{{a1, c3}, {b2, d4}}
{{a1, d4}, {c3, d4}}

I hope that's reasonably clear: cardinality defines the size of the group I wish to obtain permutations of. The first example above was: all possible pairs of groups of one element. The second example was: all possible pairs of groups of three elements (triplets). Within a single permutation, no element can be repeated, and I take only unique permutations.

I am generating all these permutations into a data-structure for later use (e.g. passing them to other software, writing them to file with their results, sending them to a DB, etc.). The code that does this is pretty simple, but unfortunately it has required me to HARDCODE the cardinality. It looks like this:

```my @data;

if (\$cardinality == 1) {

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

for (my \$j = 0; \$j < scalar(@\$permlist); \$j++) {

push @data, {
first  => [\$\$permlist[\$i]],
second => [\$\$permlist[\$j]],
};
}
}
}

if (\$cardinality == 2) {

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

for (my \$j = 0; \$j < scalar(@\$permlist); \$j++) {

for (my \$k = 0; \$k < scalar(@\$permlist); \$k++) {

for (my \$p = 0; \$p < scalar(@\$permlist); \$p++) {

push @data, {
first   => [\$\$permlist[\$i], \$\$permlist[\$j]],
second  => [\$\$permlist[\$k], \$\$permlist[\$p]],
};
}
}
}
}
}

And so on for higher cardinality. I would prefer to have a generalized way of doing this, rather than needing a separate set of for-loops for each cardinality. Is that possible? I'm turned it over in my head, and haven't found a way yet.

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?
 [Eily]: French sneeze "Atchoum", because we close our mouth when we're done :P choroba wipes the saliva from the cookies [LanX]: scary Le Pen + Melonchon had over 40% ... [vrk]: One good word for it in Finnish is pärskäys. A very wet connotation. [Eily]: somehow when googling pärskäys I got a pageful of Finnish :P [robby_dobby]: Can I go participate in an YAPC? [Eily]: (I was trying to find a translation but google translate didn't help) robby_dobby is not entirely serious [LanX]: ... thank God France doesn't have an electoral college...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2017-04-24 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (442 votes). Check out past polls.