Vortex loop - internals

Post Reply
roboto
Posts: 14
Joined: Sat Jul 31, 2010 11:45 am

Vortex loop - internals

Post 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>
User avatar
mark
Site Admin
Posts: 5513
Joined: Tue Apr 25, 2000 6:56 pm

Vortex loop - internals

Post 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>
User avatar
John
Site Admin
Posts: 2597
Joined: Mon Apr 24, 2000 3:18 pm
Location: Cleveland, OH
Contact:

Vortex loop - internals

Post 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>
John Turnbull
Thunderstone Software
roboto
Posts: 14
Joined: Sat Jul 31, 2010 11:45 am

Vortex loop - internals

Post 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.
User avatar
mark
Site Admin
Posts: 5513
Joined: Tue Apr 25, 2000 6:56 pm

Vortex loop - internals

Post 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.
Post Reply