note
davido
<p>Your intuition to use [mod://Algorithm::Combinatorics] is correct, but your choice of functions within that module was incorrect. The correct function exists, and is documented in the POD: [https://metacpan.org/pod/Algorithm::Combinatorics#variations_with_repetition(%5C@data,-$k)|variations_with_repetition].</p>
<c>
use Algorithm::Combinatorics qw(variations_with_repetition);
my @data = (1,2);
my $at_a_time = 3;
my $variations_iterator = variations_with_repetition(\@data, $at_a_time);
while (my $variant = $variations_iterator->next) {
print "@$variant\n";
}
</c>
<p>This produces:</p>
<c>
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
</c>
<p>You were so close!</p>
<p>In the future, please post the sample code. In this case I would have used it to verify that you were using the module correctly, aside from using the incorrect function.</p>
<p>Also, a lesson from high school math, and from the POD: There are n^k variations with repetition. So in your case the input set size is 2, and you're taking 3 at a time. 2**3=8. So we can validate that we got the correct number of variations by checking that we got 8, and that there are no repeats.</p>
<div class="pmsig"><div class="pmsig-281137">
<br /><p>Dave</p>
</div></div>
11132353
11132353