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" }