【问题标题】:Lua -- Split a string into a variable and backLua - 将字符串拆分为变量并返回
【发布时间】:2018-04-09 22:17:02
【问题描述】:

我想不出来这个。也许你可以试一试。

return function()
local handle = get.handle
local group1 = handle('group 1') --returns the namelabel of group 1
local group2 = handle('group 2')

for i=1,6
  do
local groupx = group1 -- convert to groupx since i also have a group2 etc.
-- some process with group1 --    
local groupx = string.sub(groupx, 6)
print(groupx) -- number 1
local groupx = groupx +1 
print(groupx) -- number 2.0
local groupx = string.format("%.f",groupx)
fdb(groupx) -- number 2
groupx = string.format('group%s', groupx)
fdb(groupx) -- shows group2
-- some process with group2 --
  end
end

它似乎不起作用。我不能通过在每个循环中添加 1 个整数来从 group1 循环到 group6。我找到了 gsub,但这主要是用文本字符串完成的,而不是在循环中每次都改变的变量。

有人可以帮我正确编码吗?非常感谢。

致以诚挚的问候,

马丁

【问题讨论】:

    标签: loops variables lua gsub dmx512


    【解决方案1】:

    您似乎混淆了变量和字符串。通常,局部变量在运行时没有字符串名称。

    这里有一些更直接的方法...

    local groups = {handle('group 1'),
                    handle('group 2'),
                    handle('group 3'),
                    handle('group 4'),
                    handle('group 5'),
                    handle('group 6')}
    for i = 1, 6 do
        local groupx = groups[i]
        -- etc.
    end
    

    或者

    for i = 1, 6 do
        local groupx = handle(string.format('group %d', i))
        -- etc.
    end
    

    或者

    local groups = {}
    for i = 1, 6 do
        groups[i] = handle(string.format('group %d', i))
    end
    for i = 1, 6 do
        local groupx = groups[i]
        -- etc.
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2019-02-05
      • 2015-01-16
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多