in reply to Parsing text file Help!

In my experience, text-parsing of a simple file boils down to four concerns:

  1. Identifying each line (and ignoring the uninteresting lines).
  2. Splitting apart each line, usually using a different set of rules for each line-type.
  3. Recognizing when you have accumulated enough information to completely act-upon.
  4. Not forgetting about the (expletive!) last record!   :-D

Deal with each one in turn.   Your “first line” is probably “one that begins with so-many consecutive digits starting at column #1.”   The “next line(s)” could simply be, in a regularly-structured file, “the next n lines following the latest first-line.”

State-machine logic can help.   You simply initialize a local scalar variable (say, $state) to some value like INITIAL_STATE, and from time to time examine that variable as you decide what to do next, and update its value to reflect what you’ve done (or seen...) recently.

I always code file-grokkers very defensively.   I figure that the program’s purpose is not only “to get information out of the file,” but also “to demonstrate that the format of the file has not changed, and that the file was built correctly by whoever built it.”   Not too many engagements ago, I was dealing with a large system that didn’t always produce the right answers and no one was quite sure why.   This system relied upon a data-feed from an external source, which, I was assured, “hadn’t changed in years.”   But it had, because at some time in the past a bug had crept into the program that the data-supplier was using to build the feed.   My “data importer from Missouri (the ‘Show Me’ $state™)” flushed it out.

Replies are listed 'Best First'.
Re^2: Parsing text file Help!
by Anonymous Monk on Nov 22, 2010 at 19:41 UTC

    How does this answer the OP? Do you ever post solutions, rather than platitudes?

      I don't like telling people how to solve their problem; it feels like doing their homework even if it isn't homework. Far better to teach them some resources so they can solve their own problem, today, tomorrow and next week.

      As Occam said: Entia non sunt multiplicanda praeter necessitatem.

        I don't like telling people how to solve their problem; it feels like doing their homework even if it isn't homework. Far better to teach them some resources so they can solve their own problem, today, tomorrow and next week.

        Some thoughts about that

        • you can't teach resources, however, you can teach skills :)
        • you can't teach while ignoring the question asked
        • you can't teach while being vague, anecdotally platitudinous
        • you can't teach something already learned
        Ok, well, actually you can teach by doing all that, but only that sundialsvc4 is super fantastic problem solver with help from state-machine logic.

        :(

        The OP had almost working code, sample input data, and used code tags!

        The OP already did everything suggested sundialsvc4 and more, only missing unpack.