my $twentyone = 21; # loop to check if we can piece together 21 cards without splitting a small order foreach (([$fivecard, 5], [$fourcard, 4], [$threecard, 3], [$twocard, 2], [$onecard, 1])) { for (my $a = $_[0]->count; $a >= 0; $a -= $_[1]) { if (($twentyone - $_[1]) > 0) { $twentyone -= $_[1]; } } }