http://www.perlmonks.org?node_id=1015873


in reply to Clubbing array elements together:

I prefer to solve such problems using a Finite-State Machine (FSM) algorithm, because it represents a generalized and easily-adaptable approach to the problem.

For example, from an initial state (that is to say, “while not in final state ...”), you read a line and have one of three possibilities:   end-of-file, a blank string, or not-all-blanks.   That would lead to final, skip_blanks, or first_string.   And so on.   The FSM can be drawn out on a piece of paper as the WikiPedia article describes.   When you determine (on paper) that the graph is entirely correct, the programming is a snap.

Since the FSM graph can be tricky to correctly design, I suggest that you build a test-suite using e.g. Test::Most to prove that it works as intended in all cases.

Replies are listed 'Best First'.
Re^2: Clubbing array elements together:
by karlgoethebier (Abbot) on Jan 29, 2013 at 20:17 UTC

    sundialsvc4:

    "I prefer to solve such problems using a Finite-State Machine (FSM) algorithm, because it represents a generalized and easily-adaptable approach to the problem."

    Sounds really cool. I'd like very much to see how you code it.

    Thanks in advance for advice and best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»