Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by frozenwithjoy (Curate)
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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2014-08-01 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (28 votes), past polls