Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How can one generate all possible combinations with reference to string positions?

by frozenwithjoy (Priest)
on Feb 20, 2013 at 18:54 UTC ( #1019826=note: print w/replies, xml ) Need Help??


in reply to How can one generate all possible combinations with reference to string positions?

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" ] ]
  • Comment on Re: How can one generate all possible combinations with reference to string positions?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019826]
help
Chatterbox?
[marto]: I think that actually caught up with me yesterday, felt a really sharp pain where my neck meets my skull.
[Corion]: marto: Ouch, yeah...
[marto]: the boys uncle will take them for a couple of hours this afternoon :)
[marto]: only at work 3 days this week, then off to Copenhagen for a wedding
[marto]: back the following Monday, potentially gone 2-3 days the next week for work
[Corion]: Oh - "Cyber Monday" week is coming up... That's why weird stuff is already discounted now...
[Corion]: marto: Oh, cool, I hope you get to enjoy the trip and Copenhagen! I found it nice (but the mermaid is underwhelming)
[marto]: the kids and their mother are coming too, actually only 9 people at the wedding in total. Still, will be fun

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2017-11-19 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (280 votes). Check out past polls.

    Notices?