Page 1 of 1

Vortex loop - internals

Posted: Wed Oct 20, 2010 2:35 pm
by roboto
Can anyone explain to me how internally vortex handles $next variable inside nested loop? This could cause serious problems especially when calling functions.

If we have two nested loops, after leaving the inner loop, $next variable value is not set to the $next variable value of outer loop.

Below is an example.

<script language=vortex>
<a name=main>
<$AS1 = "pera" "zika">
<$AS2 = "pera" "zika" "mika">
<loop $AS1>
OUTER:before: -$next
<loop $AS2>
inner:before: $next
<testfunc str=$AS2>
inner:after: $next
</loop>
OUTER:after: -$next
</loop>
</a>

<a name=testfunc str>
<rex row '.' $str>
+$next
</rex>
</a>
</script>

Vortex loop - internals

Posted: Wed Oct 20, 2010 2:57 pm
by mark
$loop and $next are set at the top of the loop and may be overwritten anywhere in the loop, as you've found. They will resync on the next iteration of the loop.

If you're relying on their values in circumstances where they may be overwritten you should save them at the top of the loop and use the saved versions:
<loop $somevar>
<local savenext=$next saveloop=$loop>
...
</loop>

Vortex loop - internals

Posted: Wed Oct 20, 2010 2:58 pm
by John
$loop and $next are set at the beginning of the loop, are still available after the </loop>.

If you need the value for a specific loop context then save the variables immediately after the <loop>, e.g.

<loop $AS1>
<$OuterNext=$next>
<loop $AS2>
<$InnerNext=$next>

Vortex loop - internals

Posted: Wed Oct 20, 2010 3:08 pm
by roboto
Why have they decided upon such a design of language? Is there a particular reason, e.g. the execution speed?
I understand that I have to use a local variable. I am more interested in the underlaying mehanism of vortex.

Vortex loop - internals

Posted: Wed Oct 20, 2010 3:48 pm
by mark
Imagine the more obvious case of

<loop $myvar>
<sql "select ...">
...
</sql>
<if $loop eq 0>
none found
</if>
</loop>

If $loop etc automatically "popped" to the outer <loop> values after the <sql></sql> you would not know how many the <sql> loop returned. One may do the same with an inner <loop> and look to $loop for the count rather than using <count> which may not be accurate if one were to <break> out of the <loop> early.