While this won't fix your logic problems, your parsing might be simpler (and easier to maintain and debug) if you used something like Parse::RecDescent.
-QM
--
Quantum Mechanics: The dreams stuff is made of
Comment on Re: Problem of Duplicating an Array while Parsing Text