Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Here's my first pass... so many loops :/ (one major problem with this approach, however, is that it becomes quite memory intensive after you have about 12 elements... so, while this works, you probably want to try to build on kennethk's approach.)
#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use Math::Combinatorics; use List::MoreUtils qw(indexes); use Data::Printer; my $line = "L1 H1 L2 H2"; my @input = split /\s/, $line; # segregate classes my @h_vals; my @l_vals; for (@input) { push @h_vals, $_ =~ /^H\d+/g; push @l_vals, $_ =~ /^L\d+/g; } # index input based on classes my @h_indexes = indexes { $_ =~ '^H\d+' } @input; my @l_indexes = indexes { $_ =~ '^L\d+' } @input; # permute values within classes my @h_perms = permute @h_vals; my @l_perms = permute @l_vals; # get all permutations of recombined classes my @perms; for my $h_cur (@h_perms) { for my $l_cur (@l_perms) { my @current; my @h = @{$h_cur}; my @l = @{$l_cur}; for (@h_indexes) { $current[$_] = shift @h; } for (@l_indexes) { $current[$_] = shift @l; } push @perms, \@current; } } p @perms; __END__ [ [0] [ [0] "L1", [1] "H2", [2] "L2", [3] "H1" ], [1] [ [0] "L2", [1] "H2", [2] "L1", [3] "H1" ], [2] [ [0] "L1", [1] "H1", [2] "L2", [3] "H2" ], [3] [ [0] "L2", [1] "H1", [2] "L1", [3] "H2" ] ]

In reply to Re: How can one generate all possible combinations with reference to string positions? by frozenwithjoy
in thread 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!
  • 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, 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
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (8)
    As of 2014-08-21 18:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (141 votes), past polls