For Each Row…End Statement (FPScript)

09.03.2021

Repeats a series of statements for all rows of a data set.

Syntax

For Each Row Counter In Data Set Do

   [Statements]

End

The syntax of the For Each Row...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

The data set whose values are to be counted. The data set cannot be a scalar value.

Statements

One or more statements that are executed for each value in Data Set.

Remarks

The For Each Row block is executed if there is at least one value 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 values in Data Set, where Counter adopts the values from zero up to the number of values minus one. The program then leaves the loop and continues the execution with the statement that follows the End statement.

If you would like to iterate all of the values of a data matrix or a signal series, you have to embed the For Each Row...End loop into a For Each Column...End loop that counts the columns. For each loop use a unique variable name as Counter.

You can change the value of Counter within a loop, but this makes comprehension and testing of the code more difficult.

Counter is often used in the loop to index a value from Data Set.

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 sets all values in a signal larger than 5.0 to the value 5.0:

Arguments Signal

For Each Row iIn SignalDo

    If Signal.Y >= 5 Then

        Signal.Y = 5

    End

End

Signal

Using event isolation, the example above can be written much more easily and efficiently:

Arguments Signal

Signal.Y[ValuesAboveLevel(Signal, 5)] = 5

Signal

Even this code can be further simplified, since there is a built-in Clip function that carries out the desired operation:

Arguments Signal

Clip(Signal, , 5)

See Also

For Each Column...End Statement

For Each Element...End Statement

For Each Value...End Statement

For...End Statement

While...End Statement

Do...While Statement

Share article or send as email:

You might be interested in these articles