LOOP command
Executes a series of ACLScript commands repeatedly on a record while a specified condition evaluates to true.
Note
The LOOP command must be enclosed inside the GROUP command.
Syntax
LOOP WHILE test command <...n> END
Parameters
Name | Description |
---|---|
WHILE test |
The test that must evaluate to true for the commands inside the LOOP command to be executed. If the test evaluates to true the commands are executed repeatedly until the test evaluates to false. |
command <...n> |
One or more commands to execute. You can enter multiple commands inside the LOOP command. Each command must start on a new line. |
END | The end of the LOOP command. |
Examples
Splitting a comma-delimited field
You have a table containing invoice data and you need to isolate specific information for invoice amounts per department. One invoice may be related to more than one department, and department codes are stored in comma-delimited format in the table.
To extract the invoice amounts per department, you:
- Use a GROUP command to process the table record by record.
- Calculate the number of departments (n) associated with each record.
- Use the LOOP command to iterate n times over the record to extract data for each department associated with the record.
COMMENT use GROUP to count commas in each department code field as a way of identifying how many departments are associated with the record "LOOP" over each record for each code in the field, with each iteration of the loop extracting the record with a single code to the result1 table END GROUP v_department_count = OCCURS(Dept_Code,',') v_counter = 0 LOOP WHILE v_counter <= v_department_count v_dept = SPLIT(Dept_Code, ',', (v_counter + 1)) EXTRACT FIELDS Invoice_Number, Amount, v_dept AS "Department" TO result1 v_counter = v_counter + 1 END END
Remarks
Tip
For a detailed tutorial covering the LOOP and GROUP commands, see Grouping and looping.
When to use LOOP
Loops are frequently used when a record contains repeated segments of data that you want to process.
How it works
Each LOOP command must specify a WHILE condition to test, and be closed with an END statement. The commands between LOOP and END are executed repeatedly for the current record as long as the specified test is true.
If the test is initially false, the commands are not executed.
Avoiding infinite loops
To avoid creating an infinite loop, make sure that the test you specify eventually returns false. You can also use the SET LOOP command to prevent infinite looping.