2

In Lua, I have a set of tables:

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.

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. How can I convert the string into the name of the table?

1
  • you can store all columns in an array colums, and by doing so you will get required column via columns[variable] without constructing string and then accessing variable via another string variable (you already store index in variable) Commented Dec 23, 2019 at 14:23

1 Answer 1

4

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. 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.

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.

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. If you have a situation like the asker, the second solution should be fine.

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

2 Comments

This works. But what if the table is local? _G, _M and _ENV give compile errors.
@JohnBig This is probably bad practice but you could assign your local table a global alias, ie MyGlobal = mylocal then access _G[MyGlobal]

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.