For Each Column…End Statement (FPScript)
Repeats a series of statements for all data series, i.e. columns, in a data matrix or all signals in a signal series.
Syntax
[Parallel] For Each Column Counter In Data Set Do
[Statements]
End
The syntax of the For Each Column...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. |
DataSet |
A data matrix or a signal series with the data series or signals to be iterated. |
Statements |
One or more statements that are executed for each data series in DataSet. |
Remarks
The For Each Column block is executed if there is at least one data series in Data Set. In this case, the program executes all of the statements in the loop with Counter equal to zero. The loop is repeated for all columns in Data Set, where Counter adopts the values from zero up to the number of columns 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 a data series from the data matrix or a signal from a signal series.
You can nest For...End loops by positioning, for instance, a For Each Row..End loop within a For Each Column..End loop. For each loop use a unique variable name as Counter.
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 Column 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 Column 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.
Note: You should avoid loops over individual values of a data set, if possible. FPScript makes it possible for you to calculate complete data sets in one statement. Loops can usually be replaced by functions for event isolation in conjunction with the Index operator. The For Each Value...End loop is the fastest FPScript loop.
Available in
FlexPro View, Basic, Professional, Developer Suite
Example
The following example calculates the surface under the curve for each signal in a signal series and returns the result as a data series:
Arguments SignalSeries
Dim Result
For Each Column i In SignalSeries Do
Result := Integral(SignalSeries).Y[-1n]
End
Result
During parallel execution of the loop, instead of using the concatenation operator, an indexed assignment has to be used:
Arguments SignalSeries
Dim Result = 0. # NumberOfColumns(SignalSeries)
Parallel For Each Column i In SignalSeries Do
Result = Integral(SignalSeries).Y[-1n]
End
Result
See Also
For Each Element...End Statement