Your intuition to use Algorithm::Combinatorics is correct, but your choice of functions within that module was incorrect. The correct function exists, and is documented in the POD: variations_with_repetition.
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_tim +e); while (my $variant = $variations_iterator->next) { print "@$variant\n"; }
This produces:
1 1 1 1 1 2 1 2 1 1 2 2 2 1 1 2 1 2 2 2 1 2 2 2
You were so close!
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.
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.
Dave
In reply to Re: Combinations with variable length
by davido
in thread Combinations with variable length
by TryingTheBest
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |