Assuming you have an input file like this one ...
horse green
horse blue
dog green
dog red
dog yellow
dog black
cat red
cat blue
cat black
bird green
bird blue
bird red
bird black
hedgehog black
hedgehog green
hedgehog red
hedgehog yellow
hedgehog cyan
rabbit black
rabbit green
rabbit yellow
rabbit cyan
you can easily get a 2-dimensional array like this (a-values on the left, showing b-matches on the right):
% perl datatest.pl
bird : black blue green red
cat : black blue red
dog : black green red yellow
hedgehog : black cyan green red yellow
horse : blue green
rabbit : black cyan green yellow
Or this one (b-values on the left, showing a-matches on the right):
black : bird cat dog hedgehog rabbit
blue : bird cat horse
cyan : hedgehog rabbit
green : bird dog hedgehog horse rabbit
red : bird cat dog hedgehog
yellow : dog hedgehog rabbit