Executes a series of ACLScript commands repeatedly on a record while a specified condition evaluates to true.
The LOOP command must be enclosed inside the GROUP command.
LOOP WHILE test command <...n> END
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.
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.|
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, extracting each code into its own record END GROUP v_department_count = OCCURS(Department_Code,',') v_counter = 0 LOOP WHILE v_counter <= v_department_count v_dept = SPLIT(Department_Code, ',', (v_counter + 1)) EXTRACT FIELDS Invoice_Number, Invoice_Amount, v_dept AS "Department" TO result1 v_counter = v_counter + 1 END END
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.