I'm sure there's a module in CPAN that could help you do this. Try searching here.
Or to go the stubborn route (or if this is homework ;) - I suggest looking into using nested datastructures such as arrays of arrays and so on, as suggested previously, which can be found here.
-------------- It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs