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
}

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


Hello - I am trying to figure out how to convert an output file to a comma
separated values file.

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.

My dilemma so basic - I know that when I open this file as <MYFILE> I can
cycle through this program as While <MYFILE> but when I do this I have to
cycle through everything at once and have a bunch of conditional statements.
To get around this, my idea is to have a main loop with two subroutines: say
FooSub and TrialSub. For the first I would split on ':' for the next I would
split on '\s+'. But how do I skip a line? I want to get past the **** . . .
line but if I use next to start the subroutine won't that create a loop that
reads no lines - perhaps an ((if $_ !~ /^\*{1,5}/) {skip} would be best and
just have each module end when it hits an '='.

Can someone give me basic advice if I am going about this in the correct
direction? I feel like I need a little nudge in the right direction before I
try to make a potentially broken idea work.

Thanks,

Tim 

____________________________________________________
Timothy B Booher, Lt USAF, AFRL/MNAC
101 West Eglin Blvd, Suite 339 
Eglin AFB FL 32542-6810 
Phone: 850-882-8302 Ext. 3360 (DSN 872-)
FAX: 850-882-2201


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to