What the others have said is good and answers your question, but if you're going to be doing a lot of stuff like this, consider using HTML::Template or some other templating system. Most support loops in the template.
Comment on Re: Constructing HTML tables with CGI.pm