Overview
 
A simple script runs from top to bottom each time a record is sent to it.But how can you initialize variables before processing starts?How can you output a grand total after all the records have been processed?

These issues and others are addressed by the step control commands.

When processing files,Parse-O-Matic performs a series of steps:
TaskInit Executes before data is read from the first input file
FileInit Executes before data is read from the current input file
Main Executes once for each record sent to the script
FileDone Executes after the last data is read from the current input file
TaskDone Executes after the last data is read from the last input file
If you are only processing a single file (i.e.you are not using wildcards to process multiple input files),there is little to distinguish TaskInit and TaskDone from FileInit and FileDone.
 
Using Step Control
 
Except for the Main step,each step appears inside a conditional block,as in this example:
TaskInit ;Start of the TaskInit step
OutEnd 'Customer Count Report'
OutEnd '---------------------'
;Report header
;Report header
End ;End of the TaskInit step
FileInit ;Start of the FileInit step
OutEnd 'Input file:'$ActualIFN
NumInpFiles =NumInpFiles+
;Output the file name
;Count this input file
End ;End of the FileInit step
CustCount =CustCount+
TaskDone
;Main step:count record
;Start of the TaskDone step
OutNull
OutEnd 'Number of input files:' NumInpFiles
OutEnd 'Number of customers:'CustCount
;Output a blank line
;Output statistics
;Output statistics
End ;End of the TaskDone step
In the example given above,the conditional code for the report header was placed in TaskInit so that the script will output it only once,even if you are processing multiple input files.
The conditional steps are optional.For example,you do not have to include FileInit in your script.
The conditional steps can appear almost anywhere in your script (though not within another conditional block).
 
FileInit and FileDone
 
The FileInit section is executed before each input file is processed.The FileDone section is executed after
each input file is processed.
You cannot combine the FileInit or FileDone commands with the If command.
 
TaskInit and TaskDone
 
The TaskInit section is executed before data is read from the first input file.The TaskDone section is executed after the last record is read from the last input file and has been processed by the Main step.

You cannot combine the TaskInit or TaskDone commands with the If command.
 
NextStep
 
The NextStep command can be used to jump out of a step (such as FileInit or Main)and proceed to the next step.
For example,if your Main step has already located the information you are seeking,there is no reason to continue reading the input file.In such case,you can execute a NextStep command to ignore the rest of the input file and proceed immediately to FileDone,as in the following example.
 
CustNum =$OutData [ 1 6 ] ;Main step:Get customer number
PhoneNum =$OutData [60 70 ] ;Main step:Get the phone number
If CustNum ='314159'NextStep ;Main step:Found the customer?
FileDone ;Start of the FileDone step
OutEnd 'Phone Number ='PhoneNum ;Output the information we sought
End ;End of the FileDone step
NextStep should not be confused with the Stop command,which causes processing to cease entirely.

NextStep is also different from Done,which skips the rest of the script and then (if used in the Main step) proceeds to process the next record from the input file.The Done command can,however,be used within a conditional step block (such as FileInit)to skip the rest of that step;in such case it will behave the same way as NextStep.
 
NextFile
 
The NextFile command jumps out of the FileInit,Main or FileDone step without processing any of the remaining file-oriented steps.For example,if you execute NextFile in the FileInit step you will skip the Main and FileDone steps.(NextFile cannot be used in the TaskInit or TaskDone steps,since these steps are not dealing with a particular file.)

NextFile is used when an input file is rejected for some reason.It may have a serious formatting error,or (if you are using wildcards)it might not precisely match the kind of file name you are looking for.

If you are indeed using wildcards,NextFile will proceed to the FileInit step for the next input file.If your script is working on the last input file,NextFile will cause the script to move to the TaskDone step.

Here is an example of NextFile,as it might be used in the Main step:
Begin $Data [1 10 ] <>'EMPLOYEE #'
LogMsg $ActualIFN 'is not formatted correctly'
HadError ='Y'
NextFile
End
In this case,the file did not contain the data we expected,so we log the error and move on to the next input file.In such case,it is a good idea to set a flag (HadError in this case)so that the TaskDone step can issue a warning:
TaskDone
If HadError ='Y'Stop 'One or more errors were detected.'>>
'Please consult the log file.'
End
Simply logging errors is no guarantee that the user will be aware that there was a problem,so we point out that the log does indeed contain some important information.

(This page is part of the online user manual for Parse-O-Matic.  Parse-O-Matic is a programmable parsing tool that can extract, manipulate, convert or mine existing data sources and turn them into importable data.  For more information on Parse-O-Matic products and conversion services, please visit www.ParseOMatic.com)