Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Combinations with variable length by davido
in thread Combinations with variable length by TryingTheBest

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-12-01 22:57 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (15 votes). Check out past polls.