You can
use PDL to simplify these calculations. As you have it, a card is just an array of four numbers mod 3. You are using 1, 2 and 3 but you can also use 0, 1 and 2.
Then a set is just three cards X, Y, Z where X+Y+Z = (0,0,0,0) mod 3.
use PDL;
sub is_a_set {
all (($_[0] + $_[1] + $_[2]) % 3) == 0;
}
sub third_card {
(pdl(3,3,3,3)-$_[0]-$_[1]) % 3;
}
my $X = pdl(0,1,2,2);
my $Y = pdl(0,2,2,1);
my $Z = pdl(0,0,2,0);
if (is_a_set($X, $Y, $Z)) {
print "$X, $Y and $Z form a set\n";
} else {
print "$X, $Y and $Z do not form a set\n";
}
print "To form a set with $X and $Y you need: ", third_card($X, $Y), "
+\n";