I don't get why and what for you need the colonna of prints in the end of your script. Let CGI.pm do that stuff for yozu, so you need fewer prints. And which errors do you get ?? Where? And why don't you use strict?
You also have some my declarations inside "For" loops, so they get a value assigned but can't carry that outside their scope.