This structure is a specialized type of loop. The SCAN command combines a FIND command with a WHILE loop. You can specify the file, key, starting value, scope, and for/while filters for the file searches. The first time the SCAN command is executed, the first appropriate record will be read. If there is a start value, the record will be found. If there isn't the program will start with the record in memory, find the first record in the file or do whatever else the programmer has specified as determined by the settings of the various options. Then, each time through the loop, the program will find the next record that matches the criteria of the different options. When the last record is found the program will transfer control to the line immediately after the ENDS command. When dealing with searches through files, SCAN/ENDS will probably become your most widely used command(s). With the FOR and WHILE filters you have absolute control over the records that are active within the loop. Any TAS Professional command may be used within the loop. The general layout of the structure is:
SCAN options
SEXIT
SEXIT_IF lexpr
SLOOP
SLOOP_IF lexpr
ENDS
SCAN | To see all the options available for this command, click here. |
SEXIT
SEXIT_IF lexpr
These elements of the SCAN/ENDS loop will allow you to exit the loop. The SEXIT option exits the loop immediately. The SEXIT_IF lexpr will exit the loop if the expression resolves to .TRUE.; otherwise nothing will happen. The lexpr must be in the form of a comparison expression. For example:
SEXIT_IF x = 1
The SEXIT and SEXIT_IF (assuming the lexpr resolves to .TRUE.) will transfer control to the next line after the ENDS command.
As many SEXIT and SEXIT_IF commands may be included in a single SCAN/ENDS loop as you desire.
SLOOP
SLOOP_IF lexpr
These elements of the SCAN/ENDS loop will allow you to transfer control to the beginning of the loop (SCAN command) without having to reach the ENDS command. The SLOOP option transfers control immediately. The SLOOP_IF lexpr will transfer control if the expression resolves to .TRUE.; otherwise nothing will happen. The lexpr must be in the form of a comparison expression. For example:
SLOOP_IF x = 1
The SCAN comparison expression is executed and then, if the expression resolves to .TRUE., the loop will continue.
As many SLOOP, SLOOP_IF commands may be included in a single SCAN/ENDS loop as you desire.
ENDS | This is the final step in the SCAN/ENDS loop. When this command is executed, control is returned to the SCAN command line. Each SCAN must have an ENDS to finish the loop. If the ENDS is not included an error message will be displayed during compilation. |
EXAMPLE
A very good example of the SCAN loop is in the Tutorial in PART 4 - CREATING A TAS Professional REPORT. In the first report sample a very simple SCAN/ENDS loop is created to output the data from the CUSTOMER file. It looks like this:
scan @cust_hndl key custcode
output_report_data rb_num 1
ends