A control structure is a component of a script that decides which direction to take based on given parameters. ACLScript provides both conditional IF logic and looping structures.
Conditional logic using IF
ACLScript implements conditional logic as an IF command and as an optional parameter on many commands in the language:
- command controls whether or not a command runs
- parameter decides which records in a table to execute the command against
The IF command
When using the IF command, you specify a conditional expression followed by the command to execute if the expression evaluates to true:
IF v_counter > 10 CLASSIFY ON customer_no
This conditional structure controls which code executes, so you can use the IF command when you want to process an entire table based on the test expression. If the expression evaluates as true, the command is run against all records in the table. For more information about the IF command, see IF command.
Many commands accept an optional IF parameter that you can use to filter which records the command is executed against:
CLASSIFY ON customer_no IF state = 'NY'
When this statement executes, the script classifies all records in the table where the value of the state field is 'NY'.
The LOOP command
The LOOP command provides the looping control structure in ACLScript.
The LOOP command must execute within the GROUP command, it cannot standalone.
This command processes the statements inside the loop for as long as the specified WHILE expression is true:
ASSIGN v_counter = 10 GROUP LOOP WHILE v_counter > 0 v_total = v_total + amount v_counter = v_counter - 1 END END
This structure iterates 10 times and adds the value of the amount field to the variable v_total. At the end of each iteration, the v_counter variable is decremented by 1 and then tested in the WHILE expression. Once the expression evaluates to false, the loop completes and the script progresses.
When the loop completes, v_total holds the sum of the 10 records' amount fields.
For more information about looping, see LOOP command.
LOOPING with a subscript
Sometimes the LOOP command does not provide the exact looping functionality you may require. In this case, you can also call a separate ACL script to execute a loop using the DO SCRIPT command: DO SCRIPT scriptName WHILE conditionalTest.
You can use one of the following common methods to control when your loop ends:
- flag the loop continues until the logical flag variable is set to FALSE
- counter the loop continues until an incrementing or decrementing variable crosses a conditional threshold
For more information about calling subscripts, see DO SCRIPT command.
You need to import all the CSV files in the C:\data folder into your project. You can use the DIRECTORY command to get a list of files from the folder, however you cannot use the IMPORT command inside the GROUP structure. You need an alternative way of looping through the table that DIRECTORY creates.
To achieve this, you create a main script that:
- Executes the DIRECTORY command and saves the results to a table.
- Gets the number of records in the table to use as a counter.
- Calls a subscript once per record in the table to execute the IMPORT command against the current record.
COMMENT Main script DIRECTORY "C:\data\*.csv" TO T_Table_To_Loop OPEN T_Table_To_Loop COUNT v_Num_Records = COUNT1 v_Counter = 1 DO SCRIPT Import_Subscript WHILE v_Counter <= v_Num_Records
COMMENT Import_Subscript OPEN T_Table_To_Loop LOCATE RECORD v_Counter COMMENT code to import CSV file in record goes here... ASSIGN v_Counter = v_Counter + 1
Variables are shared among all scripts that run in the project, so the main script calls the subscript until the value of v_Counter exceeds the value of v_Num_Records. Each time the subscript executes, it increments v_Counter.
This structure allows you to call the IMPORT command against each record while looping through the table. When the main script completes, you have imported all CSV files from the C:\data folder.