2

I am trying to remove mainMenuButtonGroup1 through mainMenuButtonGroup6.

To do this I think I need to do the following: [mainMenuButtonGroup..n] How would I do that?

local mainMenuButtonGroup1 = { 1,2 }
local mainMenuButtonGroup2 = { 3,4 }
local mainMenuButtonGroup3 = { 5,6 }
local mainMenuButtonGroup4 = { 7,8 }
local mainMenuButtonGroup5 = { 9,10 }
local mainMenuButtonGroup6 = { 11,12 }

for n = 1, 6 do
      [mainMenuButtonGroup..n] = nil
end
6
  • These variables are not global. Commented Nov 22, 2013 at 16:06
  • This is a table of display objects in the Corona SDK. In any case, _G would work, I think, if these where global but they are not :( Commented Nov 22, 2013 at 16:57
  • Do you need help iterating a table then? Or local variables? Commented Nov 22, 2013 at 17:00
  • I need help understanding how to concatenate 2 strings and then use that string to refer to a local table variable. I will then be doing "things" to said table variable. Basically, I don't want to repeat my code 6 times. Commented Nov 22, 2013 at 17:22
  • Why do you want to do '"things" to [a] variable'? Is it captured in a closure? Your code doesn't show that; And note that variables don't have types in Lua. If you want to do things to a table value, that is a different question. Your code doesn't show that either. Commented Nov 24, 2013 at 5:23

2 Answers 2

2

Based on your comments, I assume this is something like what you want to do, where t is the table that you mentioned.

for i = 1, 6 do
    local k = 'mainMenuButtonGroup' .. i
    t[k]:removeSelf()
    t[k] = nil
end

String concatenation is performed by the .. double period operator, and table indexing via . or []. In this case, you're indexing via [] because you're using a variable and not a constant name.


Edit: Put your similar local variables into a table instead. It makes it far, far easier to iterate as a group. In fact, the only way I know of to iterate locals is through the debug library.

local mainMenuButtonGroup = { {1,2}, {3,4}, {5,6}, {7,8}, {9,10} }
table.insert(mainMenuButtonGroup, {11,12})

for i = 1, 6 do
    mainMenuButtonGroup[i] = nil
end

Note, however, that setting a numeric index in a table to nil does not rearrange the table's numerically indexed values. I.e., you'll leave the table with holes. See table.remove.

Sign up to request clarification or add additional context in comments.

3 Comments

In your example k the the table I want to edit. I altered my question's code for clarity.
@Gullie667 I've edited my answer in response. Please let me know what you think.
Thanks for the answer. I guess doing it the way I originally wanted to isn't possible in LUA. I'd plus ya up but lack the rep. Thanks though.
1

Assuming the variables are global, they will be in the _G table:

for n = 1, 6 do
   for i = _G['mainMenuButtonGroup'..n].numChildren, 1, -1 do
      _G['mainMenuButtonGroup'..n][i]:removeSelf()
      _G['mainMenuButtonGroup'..n][i] = nil
   end
end

If they are local, you can use debug.getlocal, but you will have to loop over all the locals until you find the names you want. Not ideal, but possible.

1 Comment

These variables are not global. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.