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

finding all combinations

by Anonymous Monk
on Jan 13, 2008 at 20:54 UTC ( #662212=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks, I wanted to ask the following:
Say you have 3 arrays, like
@array1=(1,2,3) , @array2(7,8) and array3=(1,5,23,3);
Is there a way to find all combinations? I mean:
1-7-1, 1-7-5, 1-7-23, 1-7-3, 1-8-1, 1-8-5, 1-8-23, 1-8-3, 2-7-1, 2-7-5, 2-7-23, 2-7-3, 2-8-1, 2-8-5, 2-8-23, 2-8-3, 3-7-1, 3-7-5, 3-7-23, 3-7-3, 3-8-1, 3-8-5, 3-8-23, 3-8-3
I tried using:
for ($i==0;$i<=$array1;$i++) { for ($j==0;$j<=$array2;$j++) { for ($k==0;$k<=$array3;$k++) { print $array1[$i], "\t", $array2[$j], "\t", $ar +ray[$k], "\n"; } } }
but it only prints 1-7-1, 1-7-5, 1-7-23, 1-7-3 Could you give me any hint please?

Comment on finding all combinations
Select or Download Code
Re: finding all combinations
by Jim (Curate) on Jan 13, 2008 at 21:16 UTC
Re: finding all combinations
by aquarium (Curate) on Jan 13, 2008 at 21:21 UTC
    variable initialization/assignment is with "=" and not with "==", which is what you have inside your for loops. you're also missing a "3" in your print statement, which should end in $array3$k and not $array$k
    the hardest line to type correctly is: stty erase ^H
Re: finding all combinations
by Anonymous Monk on Jan 13, 2008 at 21:50 UTC
    Also, your loop comparison should be $i <= $#array1 or $i < @array1, not $i <= $array1, which tries to compare $i to some (probably newly created) scalar $array1.

    Strong Suggestion: always use warnings; and use strict;, which would have alerted you to some, at least, of the errors noted.

    Another Suggestion: try using the Perl-style rather than the C-style for loop:

    for my $i (@array1) { for my $j (@array2) { ... } }
Re: finding all combinations
by lima1 (Curate) on Jan 13, 2008 at 23:20 UTC
    use strict; use warnings; my @array1 = (1,2,3); my @array2 = (7,8); my @array3 = (1,5,23,3); sub to_string { return '{' . join(q{,}, @_) . '}'; } print "$_ " while glob to_string(@array1) . q{-} . to_string(@array2) +. q{-} . to_string(@array3); __END__ 1-7-1 1-7-5 1-7-23 1-7-3 1-8-1 1-8-5 1-8-23 1-8-3 2-7-1 2-7-5 2-7-23 2 +-7-3 2-8-1 2-8-5 2-8-23 2-8-3 3-7-1 3-7-5 3-7-23 3-7-3 3-8-1 3-8-5 3- +8-23 3-8-3
    Update: Ah nice jwkrahn :) Did not know $".
      Or more succinctly:
      my @array1 = (1,2,3); my @array2 = (7,8); my @array3 = (1,5,23,3); $" = ','; print "$_ " while glob "{@array1}-{@array2}-{@array3}";
Re: finding all combinations
by BrowserUk (Pope) on Jan 14, 2008 at 01:50 UTC

    Just because it uses List::Comprehensions which I recently discovered and absolutely love:

    use List::Comprehensions;; comp1 { print join '-', @_ } [1,2,3], [7,8], [1,5,23,3];; 1-7-1 1-7-5 1-7-23 1-7-3 1-8-1 1-8-5 1-8-23 1-8-3 2-7-1 2-7-5 2-7-23 2-7-3 2-8-1 2-8-5 2-8-23 2-8-3 3-7-1 3-7-5 3-7-23 3-7-3 3-8-1 3-8-5 3-8-23 3-8-3

    Or if you prefer:

    comp1 { print join '-', reverse @_ } [1,5,23,3], [7,8], [1,2,3];; 1-7-1 2-7-1 3-7-1 1-8-1 2-8-1 3-8-1 1-7-5 2-7-5 3-7-5 1-8-5 2-8-5 3-8-5 1-7-23 2-7-23 3-7-23 1-8-23 2-8-23 3-8-23 1-7-3 2-7-3 3-7-3 1-8-3 2-8-3 3-8-3

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: finding all combinations
by roboticus (Canon) on Jan 14, 2008 at 03:00 UTC
Re: finding all combinations
by brx (Pilgrim) on Jan 15, 2008 at 14:39 UTC
    Using idea of ikegami ( N-Queens problem with a regex (again) )
    With regex :
    my @array1=(1,2,3); my @array2=(7,8); my @array3=(1,5,23,3); " @array1 , @array2 , @array3 " =~ / (\S+) .*?,.*? (\S+) .*?,.*? (\S+) (?{ push @res,"$1-$2-$3"; + })(?!)/; print join ', ',@res;
Re: finding all combinations
by dokkeldepper (Friar) on Jan 18, 2008 at 12:53 UTC
    ALgorithm::Combinatorics has everything you might ever combine.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://662212]
Approved by Corion
Front-paged by SparkeyG
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (17)
As of 2014-04-23 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (552 votes), past polls