FOR EACH is used to perform different types of browse operations on the associative arrays:
- Browsing the elements of associative array.
- Browsing the values of the elements found in the associative array.
Remark: The FOR ALL, FOR EACH statements are accepted. The FOR EACH statement will be used in this documentation but it can be replaced with FOR ALL.
The FOR EACH statement can also be used to browse the .Net objects that implement the IEnumerable interface.
// Declare an associative array of integers
// Array indexed on strings and without duplicates
aaIDPerCustomer is associative array of int
aaIDPerCustomer["A"] = 55 // Add the identifier of customer "A"
aaIDPerCustomer["B"] = 321 // Add the identifier of customer "B"
aaIDPerCustomer["A"] = 56 // Modify the identifier of customer "A"
// Browse all the identifiers (syntax 1)
// 56
// 321
FOR EACH nIdentifier OF aaIDPerCustomer
Trace(nIdentifier)
END
// Declare an associative array of integers
// This array is indexed on strings with duplicates
// Ignores the case and the accented characters
aaIDPerCustomer is associative array (WithDuplicates + ccIgnoreCasse + ccIgnoreAccent) of integers
aaIDPerCustomer["E"] = 55 // add the identifier of customer "E"
aaIDPerCustomer["B"] = 321 // add the identifier of customer "B"
aaIDPerCustomer["e"] = 8 // add the identifier of customer "e"
aaIDPerCustomer["é"] = 127 // add the identifier of customer "é"
// Browse all the identifiers of customers "E" (Syntax 2)
// 55
// 8
// 127
FOR EACH nIdentifier OF aaIDPerCustomer = "E"
Trace(nIdentifier)
END
Syntax
Browsing the array elements Hide the details
FOR EACH [ELEMENT] <Variable> [, <Key> [, <Counter>]] OF <Array> [WHERE <Condition>] [<Direction>]
...
END
<FOR EACH [ELEMENT]>:
Marks the beginning of the statement block. The ELEMENT keyword is optional.
<Variable>:
Variable whose type is identical to the type of the array elements. For the arrays of classes, the variable must be a Dynamic Class variable. There is no need to declare this variable.Note: This variable is a reference to the array value. A modification of this variable in FOR EACH will also modify the value in the associative array.
<Key>:
Key of element browsed. There is no need to declare this variable.
<Counter>:
Integer variable containing the number of iterations. There is no need to declare this variable.
<Array>:
Array to browse.
<Condition>:
<Direction>:
Optional indicator for the browse direction. | |
FromBeginning (default value) | Browse the array in the order of additions into the array. |
FromEnd | Browse the array in the reverse order of additions into the array. |
Browsing the values of the "Key" elements found in the array Hide the details
FOR EACH [ELEMENT] <Variable> OF <Array> = <Key> [WHERE <Condition>] [<Direction>]
...
END
<FOR EACH ELEMENT>:
Marks the beginning of the statement block. The ELEMENT keyword is optional.
<Variable>:
Variable whose type is identical to the type of the array elements. For the arrays of classes, the variable must be a Dynamic Class variable. There is no need to declare this variable.Note: This variable is a reference to the array value. A modification of this variable in FOR EACH will also modify the value in the associative array.
<Array>:
Array to browse.
<Key>:
Value of the key for which the array elements must be browsed. For an associative array without duplicates, 0 or 1 element will be browsed. For an associative array with duplicates, 0 or N elements will be browsed.
<Condition>:
<Direction>:
Optional indicator for the browse direction. | |
FromBeginning (default value) | Browse the array in the order of additions into the array. |
FromEnd | Browse the array in the reverse order of additions into the array. |
Remarks
Syntax 1: Browsing the array elements
For each iteration, <Variable> directly refers to the current element in the array. If the value of <Variable> is modified, the current element in the array is modified.
When exiting from the loop (standard exit or via the BREAK statement), the value of the last element read is assigned to <Variable> but <Variable> does not directly refer to the array element anymore.
Example:
// Declare an associative array of integers
// Array indexed on strings and without duplicates
aaIDPerCustomer is associative array of int
aaIDPerCustomer["A"] = 55 // Add the identifier of customer "A"
aaIDPerCustomer["B"] = 321 // Add the identifier of customer "B"
aaIDPerCustomer["A"] = 56 // Modify the identifier of customer "A"
// Browse all the identifiers
// 56
// 321
FOR EACH nIdentifier OF aaIDPerCustomer
Trace(nIdentifier)
END
Syntax 2: Browsing the values of the "Key" elements found in the array
This syntax browses all the array elements with the specified <Key> value. For each iteration, <Variable> directly refers to the current element in the array. If the value of <Variable> is modified, the current element in the array is modified.
This syntax is useful when browsing associative arrays with duplicates. In an associative array without duplicates, the number of elements browsed can be 0 or 1. In an associative array with duplicates, the number of elements browsed can be 0 or N.
The array elements are browsed in the order of additions (no direction option).
When exiting from the loop (standard exit or via the BREAK statement), the value of the last element read is assigned to <Variable> but <Variable> does not directly refer to the array element anymore.
Example:
// Declare an associative array of integers
// This array is indexed on strings with duplicates
// Ignores the case and the accented characters
aaIDPerCustomer is associative array (WithDuplicates + ccIgnoreCasse + ccIgnoreAccent) of integers
aaIDPerCustomer["E"] = 55 // add the identifier of customer "E"
aaIDPerCustomer["B"] = 321 // add the identifier of customer "B"
aaIDPerCustomer["e"] = 8 // add the identifier of customer "e"
aaIDPerCustomer["é"] = 127 // add the identifier of customer "é"
// Browse all the identifiers of customers "E"
// 55
// 8
// 127
FOR EACH nIdentifier OF aaIDPerCustomer = "E"
Trace(nIdentifier)
END