Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Looping Statements

forEach()

PhixFlow provides the forEach() function to enable a script to loop over a set of statements for each entry in an Array or Recordset.

ExpressionResult
do ( $tot = 0, $tax = 0, forEach($value, invoices.amount, $tot = $tot + $value, $tax = $tax + round($value * tax.taxRate, 2) ) )

In this example, invoices is a Pipe which contains a Recordset of invoice details for a customer. invoices.amount plucks out just the amount Attribute for each invoice to create an Array of all the due amounts. tax.taxRate provides the tax rate to be applied to each invoice.

The forEach() function sets $value to the first element in invoices.amount and then processes the script lines in the remainder of the forEach() function. Note that the script does not need to be enclosed in a do() function. When the script is completed, PhixFlow assigns the next value in invoices.amount to $value and repeats the script. This processing continues until the forEach() function has processed all items in invoices.amount.

The final result should be that $tot contains the total amount due on the invoices and $tax contains the total tax value.

n..m

If you just want to iterate through a loop a known number of times, PhixFlow provides the n..m syntax to create an Array which you can then iterate over. For example

...

ExpressionResult
do ( $tot = 0, forEach($i, 1..10, $tot = $tot + $i ) )This simple example just adds up all the numbers from 1 to 10.

continue()

There may be occasions inside a loop that you want to ignore the rest of the script and just move on to the next item in the Array. In this case use the continue() function.

ExpressionResult
do ( $tot = 0, $tax = 0, forEach($value, invoices.amount, if ($value < 0, continue() ), $tot = $tot + $value, $tax = $tax + round($value * tax.taxRate, 2) ) )This example extends the case above to only add up invoices where the customer owes some amount e.g. if a credit note is found with $value < 0 then that amount is not included in the total and the continue() function is used to move on to the next item without processing the remaining script lines.
do ( $tot = 0, $tax = 0, forEach($value, invoices.amount, if ($value > 0, $tot = $tot + $value, $tax = $tax + round($value * tax.taxRate, 2) ) ) )Just to promote good scripting style, it is worth noting that the previous example was a slightly artificial way to write the expression but was done just to illustrate the use of continue(). The script here gives exactly the same result but is a little clearer.

break()

There may also be occasions inside a loop that you no longer need to process the remaining items in the Array. In this case use the break() function.

...