I hope this serves as an answer . . .

The original file is of this form:
***************************** foo *******************
thingA: 12
thingB: 23
thingC: 21
==================== trial 1 ============
                colA    colB    colC    colD
            1   23      28      273     227
        2       237     2327    11      1       
        3       0       10      22      0
        4       2       3       38      2
..etc
====================trial 2 =======
etc.
this was the solution posed . . .

-----Original Message-----
From:   Hanson, Robert [mailto:[EMAIL PROTECTED]] 
Sent:   Monday, December 31, 2001 1:21 PM
To:     'Booher Timothy B 1stLt AFRL/MNAC'; [EMAIL PROTECTED]
Subject:        RE: question-beginner's programmer's block

I'm not sure that I completely understand, so this may or may not help.
....Anyway, I hope it does.
If I understand correctly you want to grab each section and pass it to the
appropriate sub for processing.  There are lots of ways to do this, but this
way came to mind first.  It all works around a buffer and a state.  When the
*** line is seen the state is set to 1, when the ==== line is seen the state
is set to 2.  When one of these is seen it executes the dispatch() sub to
determine which sub should process the *LAST* sub seen.  If the current line
is not one of those section headers it buffers the line.
So it will run something like this...
>>***************************** foo *******************
1.      run dispatch() sending the inital state of 0 (which will do
nothing). set state to 1, go to next line.
>>                      thingA: 12
2.      buffer line
>>                      thingB: 23
3.      buffer line
>>                      thingC: 21
4.      buffer line
==================== trial 1 ============
5.      run dispatch() with a state of 1 and send the buffer (which passes
the buffer to fooSub()). set state to 2, clear the buffer, and go to the
next line.
>>
6.      buffer line
>>              colA    colB    colC    colD
7.      buffer line
>>            1 23      28      273     227
8 buffer line
>>      2       237     2327    11      1
9.      buffer line
>>====================trial 2 =======
10.     run dispatch() with a state of 2 and send the buffer (which passes
the buffer to trialSub()). set state to 2, clear the buffer, and go to the
next line.  etc, etc...

The last dispatch() is called after the loop to process the last section to
the end of file.
# UNTESTED
my $state = 0;
my $buffer = ";

while (my $line = <MYFILE>) {
if ( $line =~ /^\*+/ ) {
# this is the *** line
dispatch($state, $buffer);

# clear the buffer, set the new state # and go to the next line.
$buffer = ";
$state = 1; next;
        }
elsif ( $line =~ /^=+/ ) {
# this is a "trail" line
dispatch($state, $buffer);

# clear the buffer, set the new state # and go to the next line.
$buffer = ";
$state = 2; next;
        }

# buffer the line
$buffer .= $line;
}

# run the last section
dispatch($state, $buffer);

sub dispatch {
my ($state, $buffer) = @_;
# skip begining of file to first section
return unless ( $state );

if ( $state == 1 ) {
fooSub($buffer);
        }
elsif ( $state == 2 ) {
trialSub($buffer);
        }
}

sub fooSub {
# do stuff}
sub trialSub {
# do stuff
}

Reply via email to