|
Post by 0racle on Jan 16, 2019 8:15:38 GMT
hi all, hi AnkuLua , please help with array: Example: init a dialog first, with a list of names. If user check on a name, that name will be typed in later text box, and do some action related to that name only. My script: name = {"name1", "name2", "name3", "name4"} checkBox = {"1", "2", "3", "4"}
dialogInit() for i, v in ipairs(name) do addCheckBox(checkBox[i], name[i], false) newRow() end dialogShow -- print the checked names, checkBox value -- for i, v in ipairs(cbValue) do if (checkBox[i]) then doAllTask() print(name[i]) print(checkBox[i]) end end
Expected result: Dialog: [ ] name1 [x] name2 [x] name3 [ ] name4 Do some actions, then print the result: name2 2 name3 3 However, it gives me this result: name1 1 name2 2 name3 3 name4 4I would like to know how array can work with dialog checkbox, spinner.
|
|
|
Post by Mercobots on Jan 16, 2019 9:57:42 GMT
Remember
you assign checkBox as global var, so since your tables values ar 1,2,3... your global vars will be 1,2,3 and is not a good practice! I recomend you to set a prefix to assign your global vars "CB_" .. i
name = { "name1", "name2", "name3", "name4" }
dialogInit()
for i, v in ipairs(name) do addCheckBox("CB_" .. i, name[i], false) newRow() end dialogShow() -- print the checked names, checkBox value -- for i, v in ipairs(name) do if (_G["CB_" .. i]) then --doAllTask() print(name[i]) end end
|
|
|
Post by 0racle on Jan 16, 2019 10:27:37 GMT
Remember you assign checkBox as global var, so since your tables values ar 1,2,3... your global vars will be 1,2,3 and is not a good practice! I recomend you to set a prefix to assign your global vars "CB_" .. iname = { "name1", "name2", "name3", "name4" }
dialogInit()
for i, v in ipairs(name) do addCheckBox("CB_" .. i, name[i], false) newRow() end dialogShow() -- print the checked names, checkBox value -- for i, v in ipairs(name) do if (_G["CB_" .. i]) then --doAllTask() print(name[i]) end end Got it. Many thanks!!!
|
|
edison12344
Contributors
Target Mission: Help Gamers
Posts: 142
|
Post by edison12344 on Jan 16, 2019 12:00:54 GMT
You can minimize your for loop into
for i,#name do -- #name is the length of array name[] addCheckBox(variables[i],name[i],false) end
|
|
|
Post by AnkuLua on Jan 16, 2019 13:47:55 GMT
|
|