Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Parse File With Sub While Loops

by fsn (Friar)
on Sep 16, 2002 at 21:51 UTC ( #198381=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://198381]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-12-05 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?