简体   繁体   English

在Lua中连接功能名称

[英]Concatenate a Function Name in Lua

This is probably really simple but I'm new so here we go.. 这可能真的很简单,但是我是新手,所以我们开始吧。

How would I code this: 我该如何编码:

local color1 = { 255,0,0 }
local color2 = { 1,200,1 }
local color3 = { 2,2,150 }
for i = 1, 3 do 
    local x = "color" .. i[i]
    print( x )
end

What I am looking for as output 我在寻找什么作为输出

255  
200  
150 

The simplest solution would be to put the color info in an array 最简单的解决方案是将颜色信息放入数组中

local colors = {
    { 255,0,0 },
    { 1,200,1 },
    { 2,2,150 },
}

-- Iterating by hand:
for i=1, #colors do
    local rgb = colors[i]
    print(rgb[i])
end

-- ipairs is another way to do the same thing
for i, rgb in ipairs(colors) do
    print(rgb[i])
end

If the color1 , color2 and color3 tables are static; 如果color1color2color3表是静态的; you can try this approach: 您可以尝试以下方法:

local color1, color2, color3 = { 255,0,0 }, { 1,200,1 }, { 2,2,150 }
color = { color1 = color1, color2 = color2, color3 = color3 }
for i = 1, 3 do 
    local x = color["color"..i][i]
    print( x )
end

Output: http://codepad.org/qL5K3jNq 输出: http : //codepad.org/qL5K3jNq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM