Re: breaking a text file into a data structure -- best way?by rubasov (Friar)
|on Apr 09, 2010 at 16:59 UTC||Need Help??|
Let me try to help:
First analyze the structure of your input, name the parts of it. This input consist of lines, each line consists three fields: a prefix, a separator and a text field. Consecutive prefixes form a block and consecutive blocks form a prefix alphabet.
Second, answer this question: is the input parsable line-by-line or you have to look around (at a certain point) to decide what-is-what? The former answer is typically resulting more efficient programs (but it is not possible for all types of input) and the latter is generally easier to code, but requires to hold more of your input in memory. (I decided to choose the line-by-line approach by storing the previous prefix only beyond the current line.)
Then constrain yourself to go through your input line-by-line and ask yourself: what are the states (or state transitions) determining what should I do?
How to map these states to relations between lines? By comparing the prefix of the current and the previous line.
What is the tool to express these relations between the lines? Alphabetical comparison. The mapping is (cf. with the previous listing):
What should I do at each state transition?
Now try to write it again and if you're stuck, come back and look at this:
Of course this is only one approach, but the clearing of concepts, methodical thinking of the mechanical way to solve a problem always helped me.
And in general: practice and practice more. Read books, read the code of others (not just glance over, but change them, understand them), read the problems of others and try to solve them without looking at the solution posted by others.Cheers