in reply to Parsing text file Help!
In my experience, text-parsing of a simple file boils down to four concerns:
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 | |
by TomDLux (Vicar) on Nov 23, 2010 at 03:44 UTC | |
by Anonymous Monk on Dec 24, 2010 at 21:43 UTC |