Issue with setting local variable in for loop (FL)

fl() isn’t a loop statement, it’s a function. It expects a formula string as the loop body argument, and it then parses and evaluates that formula string. Your lv() call is not in a string, and so before fl() runs, lv(x, lv(x) + 1) is evaluated, resulting in 2 being stored in local x, and your loop body argument being an empty string. The loop then prints an empty string 10 times, and you finally read local x, which is still 2.

So, to actually make this happen: For syntax, you can use #x to read the local, and you don’t need quotation marks around x, you can leave it quoteless. You’ll also, unfortunately, run into this bug:

Working with all of that:

$fl(1, 10, "i+1", 
    "lv(x, if(i = 1, 1, #x + 1)) + if(i = 10, #x)"
)$

Notice that the entire loop body is a string, and 1, the value for the first iteration, is resolved inside fl(), to work around the bug - also, the final result is returned from the final iteration, because apparently variables don’t smothly transfer out of loops either. It’s a mess.

If you needed to use quotation marks inside the loop body, you can concatenate them using + tc(utf, 22) +, or use a placeholder like ' and then replace that placeholder with tc(reg, "loop + ' ' + body", ', tc(utf, 22)).

If you’d like to learn more about formulas:

1 Like