简体   繁体   English

Lua中将字符串转换为变量名

[英]Convert string to variable name in Lua

In Lua, I have a set of tables:在 Lua 中,我有一组表:

Column01 = {}
Column02 = {}
Column03 = {}
ColumnN = {}

I am trying to access these tables dynamically depending on a value.我正在尝试根据值动态访问这些表。 So, later on in the programme, I am creating a variable like so:所以,稍后在程序中,我创建了一个像这样的变量:

local currentColumn = "Column" .. variable

Where variable is a number 01 to N.其中变量是一个数字 01 到 N。

I then try to do something to all elements in my array like so:然后,我尝试对数组中的所有元素执行某些操作,如下所示:

for i = 1, #currentColumn do
    currentColumn[i] = *do something* 
end

But this doesn't work as currentColumn is a string and not the name of the table.但这不起作用,因为 currentColumn 是一个字符串而不是表的名称。 How can I convert the string into the name of the table?如何将字符串转换为表名?

If I understand correctly, you're saying that you'd like to access a variable based on its name as a string?如果我理解正确,您是说您想根据名称作为字符串访问变量? I think what you're looking for is the global variable, _G .我认为您正在寻找的是全局变量_G Recall that in a table, you can make strings as keys.回想一下,在表中,您可以将字符串作为键。 Think of _G as one giant table where each table or variable you make is just a key for a value.将 _G 视为一个巨大的表,其中您创建的每个表或变量只是一个值的键。

Column1 = {"A", "B"}
string1 = "Column".."1" --concatenate column and 1. You might switch out the 1 for a variable. If you use a variable, make sure to use tostring, like so:
var = 1
string2 = "Column"..tostring(var) --becomes "Column1"
print(_G[string2]) --prints the location of the table. You can index it like any other table, like so:
print(_G[string2][1]) --prints the 1st item of the table. (A)

So if you wanted to loop through 5 tables called Column1,Column2 etc, you could use a for loop to create the string then access that string.因此,如果您想遍历名为 Column1、Column2 等的 5 个表,您可以使用 for 循环来创建字符串,然后访问该字符串。

C1 = {"A"} --I shorted the names to just C for ease of typing this example.
C2 = {"B"}
C3 = {"C"}
C4 = {"D"}
C5 = {"E"}
for i=1, 5 do
local v = "C"..tostring(i)
print(_G[v][1])
end

Output输出

A
B
C
D
E

Edit: I'm a doofus and I overcomplicated everything.编辑:我是个傻瓜,我把一切都复杂化了。 There's a much simpler solution.有一个更简单的解决方案。 If you only want to access the columns within a loop instead of accessing individual columns at certain points, the easier solution here for you might just be to put all your columns into a bigger table then index over that.如果您只想访问循环中的列而不是在某些点访问单个列,那么这里更简单的解决方案可能只是将所有列放入一个更大的表中,然后对其进行索引。

columns = {{"A", "1"},{"B", "R"}} --each anonymous table is a column. If it has a key attached to it like "column1 = {"A"}" it can't be numerically iterated over.
--You could also insert on the fly.
column3 = {"C"}
table.insert(columns, column3)
for i,v in ipairs(columns) do
print(i, v[1]) --I is the index and v is the table. This will print which column you're on, and get the 1st item in the table.
end

Output:输出:

1   A
2   B
3   C

To future readers: If you want a general solution to getting tables by their name as a string, the first solution with _G is what you want.致未来的读者:如果您想要一个通用的解决方案来按名称作为字符串获取表,那么第一个带有 _G 的解决方案就是您想要的。 If you have a situation like the asker, the second solution should be fine.如果你有像提问者这样的情况,第二种解决方案应该没问题。

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

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