[SOLVED][SCRIPTING] Loop optimization

I asked a question about Lua loops at stack exchange:

I thought some of you might be interested,

1 Like

you know that, depending of your needs, you can also create a function with what is done inside the loop and do a foreachi() or foreach() ?

testtable = {aaa = "aaa", bbb = "bbb" ; 1, 2, 3, 4 }

function f_test(_index, _value)
    print(_index.."=".._value)
end

foreachi(testtable, f_test)

foreach(testtable, f_test)

foreachi should print :
1=1
2=2
3=3
4=4

foreach should print (perhaps not in this order) :
aaa=aaa
bbb=bbb
1=1
2=2
3=3
4=4

foreachi() and foreach() are not available in every scope, so I got out of the habit of using them.

You’ve found a scope where getn() is available but not foreachi()/foreach() ?
If that’s the case, that’s even worse than what I thought during my UI tests ! (where all of them are not available directly in the UI screens files)

No, getn() isn’t always available either.

Sorry if what I said was confusing. What I meant was : are you sure that getn() can be available if foreach()/foreachi() is not ? Seemed linked in all my UI tests

No, getn() is not available either.