|XP is just a number
Why try and parse this sort of thing when Perl's not-exactly-secret-weapon CPAN has plenty well tested modules that'll do all this for you?
HTML::Parser would be one place to start - it includes modules to slice and dice your HTML in several different ways - check the README. As far as I can see, you should be able to replace the snippet you've posted with these modules.
Update: Is that even valid HTML? It looks pretty horrid either way. A much nicer way of doing it would be:
Further update:Yes, it is valid HTML. Still looks hideous :-)