For Each Element…End Statement (FPScript)
Repeats a series of statements for all elements in a list.
Syntax
[Parallel] For Each Element Counter In List Do
[Statements]
End
The syntax of the For Each Element...End statement consists of the following elements:
Element |
Description |
---|---|
Counter |
Variable that is used as a loop counter. You do not have to declare the loop counter with Dim. |
List |
The list with elements to be counted. |
Statements |
One or more statements that are executed for each element in List. |
Remarks
The For Each Element block is executed if there is at least one element in List. In this case, the program executes all of the statements in the loop with Counter equal zero. The loop is repeated for all elements in List, where Counter adopts the values from zero up to the number of elements minus one. The program then leaves the loop and continues the execution with the statement that follows the End statement.
You can change the value of Counter within a loop, but this makes comprehension and testing of the code more difficult. Counter is frequently used within the loop to index an element from List.
If you prepend the Parallel keyword, the number of iterations in FlexPro Professional and Developer Suite will take place concurrently instead of consecutively. FlexPro will then allocate execution to the highest number of concurrent threads depending on the number of processor cores available. This results in a corresponding multiplication of the processing speed, as long as it is not limited by other factors such as reloading large data sets from the hard drive.
Please note that there are some consequences when executing concurrently:
•For the loop counter and local variable, which you declare inside the Parallel For Each Element block, an independent instance is created for each thread. Access to this type of variable from outside the block is not allowed.
•A local variable that you declare above the Parallel For Each Element block is used jointly by all threads. If you select write access to this type of variable within the block, its value will change immediately for all other threads as well.
•Since the order of the number of iterations is indefinite, concepts such as gathering individual results cannot be used with the bundling operator. You should dimension the result sufficiently before the loop instead and then enter the individual results using the indexed assignment.
•The Break statement is not permitted in a parallel loop.
Available in
FlexPro View, Basic, Professional, Developer Suite
Example
The following example provides a list with the derivatives of the signals in the list that is passed as the argument:
Arguments ArgList
Dim ResultList = List(ArgList, "Derivative")
Parallel For Each Element i In ArgList Do
ResultList. = Derivative(ArgList.)
End
ResultList
See Also
For Each Column...End Statement