use strict; use List::Util qw(shuffle); # construct a standard deck my @card; foreach my $suit (qw(H C S D)) { foreach my $value (2..10, qw(J Q K A)) { push @card, $value . $suit; } } # now shuffle us up a pile of cards, perhaps composed of any # number of decks my @pile; my $n = 4; # number of decks in shuffled pile push @pile, @card for (1..$n); @pile = shuffle @pile; # i think i'll try to generalize the algorithm such that it organizes # the pile into distinct groups that each have only one of each item. # in this test case, it will organize the pile into the appropriate # number of decks, each containing only one of each card. my $group = [[]]; while (my $item = shift @pile) { # search the current groups and add if item not found my $found; foreach my $i (0..$#$group) { unless ( grep { $item eq $_ } @{$group->[$i]} ) { push @{$group->[$i]}, $item; $found = 1; last; } } # if not found, make a new group with that item in it push @$group, [$item] unless $found; } # now print the results to see our handiwork foreach (0..$#$group) { print join(" ", @{$group->[$_]}), "\n\n" } #### 10S QH 9S 6D 4S 10H JD JS KS AD 9C 7C KH 6S 8S 4D 10D 5D 10C QC 7S 9D 7H QS KD 2D 6H JC 5H 3D 8H JH 2S 9H 5C AC 2H AS 3C 8C KC 4C 5S 7D 3H QD 8D 3S 2C AH 4H 6C 9C JS KH KS 4D QH 10S 7S AD 4S QS QC 5D 5H 8S 6H JD KD 9S 10D 5C 10H 8H 10C AS JH 2D 7D 6S 3H KC 3C 9H AC 9D 3D 4C 2H QD AH 8C 2C 3S JC 2S 7H 4H 6D 7C 6C 8D 5S 10S 4S QS 6H 8S 7S KS 10C 7D KD 4D AD JH 9C 9S QH KH 3D 5D 9D 6S 3H QD 5H 2H JC 8C 2D 2S JD 3C 3S 10H AH AS 6D 2C QC 8H 4C 7C 9H 10D 4H JS AC KC 5C 8D 6C 5S 7H QD 4S 7S JC JH AD 3H 8S QH 3D QS KS 4D 10C KD 9D 6H 3S QC 6S 6D 8C 10S 8H 7C AS 2D 2S AH 7D JD 5C 9C KC 8D 9H 10H KH 3C 2H 4H 4C 5D JS 10D 2C 5S 9S AC 5H 7H 6C