Your formatting is pretty much screwed up. To be honest, I don't see what you are saying. Try to fix it and I'll do my best to help you. In the meantime I'd recommend the HTML::TokeParser Tutorial. In general, if you do extensive HTML or XML processing, consider using a module.
Cheers, CombatSquirrel.