in reply to
Perl: Extracting specific text from a .txt file and outputting into a new format
It might be useful to apply “state machine” logic here. Or even to approach the parsing task using a tool such as Parse::RecDescent (which has become a very well-known tool to me as of late...).
Basically, it seems that the best way to describe this problem is that “the proper interpretation of what is in front of me now, depends upon what surrounds it; on what has come before.” That contextual knowledge can be represented in a “state.” And, the greater task might well be expressable using a grammar, hence my suggestion of a true parser.