http://qs321.pair.com?node_id=198381


in reply to Parse File With Sub While Loops

You give too little information for me to make a more elaborate suggestion, but I'll try to give you a generic one.

When processing a file like this, I try to make just one while loop, and then have some kind of statemachine-ish construction to do the actual work for me. The important thing is to avoid reading from the file in more than one place, like in the main loop and then a sub loop that exhausts some data, since that always seems to give me problems where I must reinsert data back into the buffer in some way, or handle special cases.

So, this is my general design principle (in perl-ish pseudocode):

my $state; open SESAME, "infile"; while (<SESAME>) { # Setting the "states" of the "state machine" if ( $_ =~ /FH/) { $state = "FH" } if ( $_ =~ /BH/) { $state = "BH"} . . # Do different things with the data depending on the # settings of the "state machine" if ( $state eq "FH" ) { # do this } if ( $state eq "BH" ) { # do that } . . } close SESAME;
No idea if this helps you.