Nested loops
Alex Tweedly
alex at tweedly.net
Tue May 17 07:49:20 EDT 2022
Direct equivalent is probably
repeat with i = 0 to myArrayLength-1
put i-1 into j
if j < 0 then put myArrayLength-1 into j
.....
end repeat
OR, if you prefer a single-statement version
repeat with i = 0 to myArrayLength-1
put (i + myArrayLength-1) mod myArrayLength into j
.....
end repeat
OR - most efficient but risky - i.e. a bad choice iMHO :-)
put myArrayLength-1 into j
repeat with i = 0 to myArrayLength-1
.....
put i into j -- NB at the very end of the loop body
end repeat
This is risky in case you ever do a "next repeat" inside the loop body !!
Alex.
On 17/05/2022 07:35, jbv via use-livecode wrote:
> Hi list,
>
> What is the best and most efficient way to write the following
> javascript loops in LC ?
>
> for (let i = 0, j = myArray.length - 1; i < myArray.length; j = i++) {
> }
>
> This goes beyond simple nested loops.
> So far, because of lack of time, I have pre-calculated the successive
> values of i and j, and have put them into lists of words :
> 0 1 2 3 4 5
> 5 0 1 2 3 4
> it remains fast enough for what I need to do, but there must be a more
> elegant approach...
>
> Thanks.
> jbv
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list