robertw has asked for the wisdom of the Perl Monks concerning the following question:

Dear perl monks, I am sorry to post this second question so rapid after the first, i am dying to know the answer to this one as well: i am trying to perceive Games::Cards::Poker, understand it to some extent . I want to have an array that has all possibilities of 56*55*54*53*52, I want to have every single combination of 5 cards out of 56

my @total = ("1-S","2-S","3-S","4-S","5-S","6-S","7-S","8-S","9-S","10-S","11-S","12-S","13-S","14-S","1-H","2-H","3-H","4-H","5-H","6-H","7-H","8-H","9-H","10-H","11-H","12-H","13-H","14-H","1-R","2-R","3-R","4-R","5-R","6-R","7-R","8-R","9-R","10-R","11-R","12-R","13-R","14-R","1-K","2-K","3-K","4-K","5-K","6-K","7-K","8-K","9-K","10-K","11-K","12-K","13-K","14-K");

I got the perl module Set::CrossProduct but i don't know how to utilize it in such a way that I get 56*55*54*53*52 combinations of 5 cards in one array, the problem i think is that i can make it choose an item out of five lists but then it would choose the same item twice since i cant delete one out of the second list it has to go through since it chooses a different item every time. Could someone maybe tell me how to do this particular thing with this module or recommend another one? Thank you so much in advance

Replies are listed 'Best First'.
Re: How to get all combinations
by tobyink (Canon) on Jul 06, 2012 at 22:22 UTC

    You want Algorithm::Combinatorics.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: How to get all combinations
by BrowserUk (Patriarch) on Jul 06, 2012 at 22:32 UTC

    Is this what you are after?

    #! perl -slw use strict; use Algorithm::Combinatorics qw[ combinations ]; my @total = ( "1-S","2-S","3-S","4-S","5-S","6-S","7-S","8-S","9-S","10-S","11-S","1 +2-S","13-S","14-S", "1-H","2-H","3-H","4-H","5-H","6-H","7-H","8-H","9-H","10-H","11-H","1 +2-H","13-H","14-H", "1-R","2-R","3-R","4-R","5-R","6-R","7-R","8-R","9-R","10-R","11-R","1 +2-R","13-R","14-R", "1-K","2-K","3-K","4-K","5-K","6-K","7-K","8-K","9-K","10-K","11-K","1 +2-K","13-K","14-K" ); my $iter = combinations( [0..55], 5 ); print join ' ', @total[ @$_ ] while defined( $_ = $iter->next ); __END__ C:\test>junk2 1-S 2-S 3-S 4-S 5-S 1-S 2-S 3-S 4-S 6-S 1-S 2-S 3-S 4-S 7-S 1-S 2-S 3-S 4-S 8-S 1-S 2-S 3-S 4-S 9-S 1-S 2-S 3-S 4-S 10-S 1-S 2-S 3-S 4-S 11-S 1-S 2-S 3-S 4-S 12-S 1-S 2-S 3-S 4-S 13-S 1-S 2-S 3-S 4-S 14-S 1-S 2-S 3-S 4-S 1-H 1-S 2-S 3-S 4-S 2-H 1-S 2-S 3-S 4-S 3-H 1-S 2-S 3-S 4-S 4-H ...

    If so, then your calculation is wrong. It results in 3.8 million combinations, not the 458 million that your calculation would produce.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.

    The start of some sanity?

      I copied the script, there seems to be something goofy with my algorithm combinatorics locations:

      #! perl -slw use strict; use lib '/opt/perl5/perlbrew/bin/perlbrew/perls/perl-5.14.0/lib/site_p +erl/5.14.0/darwin-2level' use Algorithm::Combinatorics qw[ combinations ]; my @total = ( "1-S","2-S","3-S","4-S","5-S","6-S","7-S","8-S","9-S","10-S","11-S","1 +2-S","13-S","14-S", "1-H","2-H","3-H","4-H","5-H","6-H","7-H","8-H","9-H","10-H","11-H","1 +2-H","13-H","14-H", "1-R","2-R","3-R","4-R","5-R","6-R","7-R","8-R","9-R","10-R","11-R","1 +2-R","13-R","14-R", "1-K","2-K","3-K","4-K","5-K","6-K","7-K","8-K","9-K","10-K","11-K","1 +2-K","13-K","14-K" ); my $iter = combinations( [0..55], 5 ); print join ' ', @total[ @$_ ] while defined( $_ = $iter->next );

      Combinatorics.pm:9: this one appears to be wrong: XSLoader::load('Algorithm::Combinatorics', $VERSION);

      getallcombos.pl:5: Compilation failed in require getallcombos.pl:5: BEGIN failed--compilation aborted this sentence appears to be wrong use Algorithm::Combinatorics qw[ combinations ];
      Everything is installed correctly, however it seemed in the wrong directory so I screwed something up, I think some things are out of place, would you know how I can solve this problem?

        Everything is installed correctly

        Sorry, but that is obviously not the case.

        Also, I know nothing of perlbrew or darwin (is that Apple?), so I cannot begin to suggest how to fix your installation.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.

        The start of some sanity?

        In the library I use there is only combinatorics.pm, I also have a combinatorics directory that contains an bundle, and a bs file which i dont refer to, this directory is located in the place it usually installs the .pm file:S, I hope this helps for clarity
Re: How to get all combinations
by Marshall (Canon) on Jul 06, 2012 at 22:31 UTC