How do I make a dynamic variable name in Lua?

前端 未结 3 1218
走了就别回头了
走了就别回头了 2020-12-11 18:14

I am new to Lua and am having some difficulties:

I am trying to create dynamic variable names:

local tblAlphabet = {\"a\",\"b\",\"c\",\"d\",\"e\",\"f         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 18:53

    The variables you create are first-class values that do not have names.

    You can assign them to variables which have names. Either local variables, or in this case (since you want to do it in a loop), key names in a table (either the globals table, or a table you create).

    You do not need a data table to create your button names, since they follow a simple pattern.

    t = {}
    for b = string.byte('a'), string.byte('z') do
        c = string.char(b)              -- 'a' to 'z'
        t['button'..c] = ui.newButton() -- something like this
    end
    

提交回复
热议问题