LOOP - iterate through variables

SYNOPSIS

<LOOP [SKIP=s] [MAX=m] [REV] $var1 ...>
  ... statements ...
</LOOP>


DESCRIPTION
The LOOP statement iterates through the values of the given variables, executing the given statements each time. Inside the LOOP, any reference to a LOOP variable returns the current value of the variable, instead of the first (0th) value: in the nth loop iteration, the nth value of each variable is used. The loop continues until the last value of the variable with the most values is iterated, or MAX iterations have occurred. MAX is optional; its argument is an integer literal or variable.

If SKIP is given, then the first s values of the variables are skipped; i.e. iteration (and the value of $loop, unlike with other looping statements) starts with the sth value instead of the 0th.

Inside the LOOP, the special variable $loop is set at the start of each iteration to the current index (starting from 0, plus SKIP) into the loop variables. When the LOOP finishes (via </loop>), $loop is set to the number of iterations (plus SKIP). This provides an easy way to enumerate or count values inside the LOOP.

The variable $next is set to the "next" loop value each iteration. Inside the loop, this is $loop + 1: an easy way to count values starting from 1 instead of 0. When the LOOP finishes (via </loop>), $next is equal to $loop: it is the value to use for SKIP in a new LOOP to continue iteration at the next value.

If the REV flag is given, then the variables are iterated in reverse order, from last to first values. However the values of $loop and $next are still incremented forwards.

If a BREAK statement is encountered inside the loop, the LOOP is exited at that point, as if none of the variables had any further values. The $loop and $next variables, however, will have whatever values they had at that point; they will not be set by </loop>.

The single-value nature of iterated variables applies globally. If a function is called from within a LOOP, references to the current LOOP's variables (if global) still return just the single current value. Variables not listed in the current LOOP tag, however, still return all their values inside a LOOP.

Modifying a LOOP variable inside the loop will only change the current value of the variable; see Variable Assignment (here). This can happen not only with an explicit variable assignment, but indirectly if a function or statement is called that sets $ret, for example. Thus LOOPing over $ret is generally not advised, as nearly any function called inside the loop will attempt to modify $ret - possibly losing type information and/or values. This is also true of other statements in Vortex that put variables in a loop, such as SQL. Use the texis.ini setting [Texis] Warn Ret Loop or the --warn-ret-loop command-line option (here) to warn when such code is compiled.


EXAMPLE
This example generates radio buttons for a list of colors (essentially the <radiobutton> function):

<$colors = Red    Orange Yellow Green  Blue   Violet>
<$rgb    = FF0000 FFA500 FFFF00 00FF00 0000FF EE82EE>
<LOOP $colors $rgb>
  <input type="radio" name="rgb" value="$rgb"/> $colors
</LOOP>


CAVEATS
The REV flag was added in version 3.0.947100000 20000105.

LOOPs can be nested; however the same LOOP variable should not be used in an inner LOOP since it is already a single value.

The values of "short" LOOP variables, i.e. ones with fewer values than others in the same LOOP, appear as empty when iterated past their end.


SEE ALSO
SQL, BREAK


Copyright © Thunderstone Software     Last updated: Oct 24 2023
Copyright © 2024 Thunderstone Software LLC. All rights reserved.