Hi Perl Monks,

I have a string $a= “L1 H1 L2 H2”; with two Ls at positions 1, 3 and two Hs at positions 2, 4, respectively. The Ls can interchange between them i.e. say for position 1 , L1 may change to L2 but not to H. Similarly, the two H can interchange between them at positions 2 & 4. Finally these four elements will produce a set of 4 sequences like L1H1L2H2; L2H1L1H2; L1H2L2H1; L2H2L1H1; taking the fixed positions for L (1,3) and H(2,4) into consideration. I am at my wit’s end to write a script which can find all these four combinations. The given string just consists of 4 positions and two groups i.e. L & H each with 2 elements and so it can be worked out easily. If the string contains, say 500 positions with 10 main groups and each group with varying number of elements, it will be much easier to find all the possible combinations using a perl script. I have gone through the posts relating to permutations, combinations and AoA but I couldn’t find a text which can solve this problem. I am looking forward to the suggestions of Perl Monks regarding this problem. The results might look like:

Number of Combinations: 4 Combinations are: L1H1L2H2 L2H1L1H2 L1H2L2H1 L2H2L1H1

In reply to How can one generate all possible combinations with reference to string positions? by supriyoch_2008

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.
  • 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, details, 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, summary, 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:
    & &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.