A more generalized way to handle such requirements is with a FiniteState Machine (FSM) approach.
The algorithms consider, not only the current line of input, but the $state that the FSM is “in” at the time,
where the current value of $state is determined by recent history of lines seen.
Sounds cool. I found a lot of interesting hits using Super Search:
 Re: Comparing two hasheshelp: "I often find it useful to describe logic like this in terms of a finitestate machine (FSM)"
 Re: Spliting file + removing column: "For this, I use “finitestate machine (FSM)” logic"
 Re: ADSI groups users: "I naturally look at such problems with an eye toward socalled finite state machine logic"
 Re: IO::Socket client does not detect when server network connection dies: "Logic like this is sometimes well designed using Finite StateMachine (FSM) logic"
 Wisdom on how to build a "stressful simulation test" with Selenium & POE: "Each actor is basically an individual finitestate machine"
 Re: A way to avoid repeated conditional loops: "Call it “a flag variable” if you want to, but this is a classic place for a finitestate machine (FSM) algorithm"
 Re: Perl/Tk code structure: "A typical design for the shepherd process is a FiniteState Machine (FSM), or more likely, two FSMs"
 Re: how did blocking IO become such a problem?: "The entire life cycle of a request, and much of the outer requesthandling heuristics, is most easily described using a finitestate machine (FSM) algorithm"
 Re: Clubbing array elements together:: "I prefer to solve such problems using a FiniteState Machine (FSM) algorithm"
 Re: File Find/Replace with the replacement coming from part of earlier matched string: "This is an absolutely classic case for a “finitestate machine (FSM)” algorithm"
 Re: How to check if successfully logged in?: "it must be a finitestate machine (FSM) design, because in the final analysis the host website is driving the bus ... Fact of the matter is, a production mechanizescript is often two FSMs"
 Re: Reading concurrently two files with different number of lines: "It might be useful for you to look at the concept of FiniteState Machine (FSM) algorithms as a source of ideas for generalized solutions to these problems"
 Re: RFC: Simulating Ruby's "yield" and "blocks" in Perl: "these can be used to implement finitestate machines (FSMs)"
 Re: Selecting HL7 Transactions: "this sort of thing is mosteasily handled by finitestate machine (FSM) techniques"
 Re^2: Too much recursion: "For dealing with very complicated inputs, the notion of a FiniteState Machine (FSM) can be useful"
 Re: Sorting through a file with multiple tables and extracting data: "The general approach is that of a finitestate machine (FSM)"
 Re: Is this a simple, robust, and maintainable design?: "sounds like a Finitestate machine. If you haven't coded a finite state machine in Perl before, this article may be helpful."
All these glowing endorsements have got me excited, yet I couldn't
find any sample code in any of these nodes.
So I was wondering if you could
post some of the excellent FSM code you've
implemented over the years?
It would really help me to better understand FSMs.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
 a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:

For: 

Use: 
 &   & 
 <   < 
 >   > 
 [   [ 
 ]   ] 
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.

