0

I'm using Lua with IUP and have a number of pairs of IUP handles thus:

UseField1 = iup.toggle {blah blah blah}
Field1Label = iup.text {blah blah blah}

The number of field pairs (maxFields) is currently 5 but may vary.

At various places in my Lua programme, I need to do something like:

for N in 1,maxFields do
    If UseFieldN.value =="ON" then
      DoSomethingWith(FieldNLabel.value, N)
    end
end

I know I can't construct dynamic variable names, but is there a way to write this as a concise loop , rather than:

If UseField1 =="ON" then DoSomethingWith(Field1Label.value, 1) end
If UseField2 =="ON" then DoSomethingWith(Field2Label.value, 2) end
etc
ColeValleyGirl
  • 577
  • 15
  • 40

1 Answers1

1

I suggest using Lua tables.

t = {}
t.UseField1 = iup.toggle {blah blah blah}
t.Field1Label = iup.text {blah blah blah}
...

or

t[1] = iup.toggle {blah blah blah}
t[2] = iup.text {blah blah blah}
...

Then loop over the elements of the table:

for index,elem in pairs(t) do 
    If elem.value == "ON" then
      DoSomethingWith(elem.value, N)
    end
end

or

for index,elem in ipairs(t) do -- when using only numeric indices
    If elem.value == "ON" then
      DoSomethingWith(elem.value, N)
    end
end
Antonio Scuri
  • 1,046
  • 6
  • 10