0

Before I start, I'll say I am a beginner at Lua, so may not know all the correct terms, but I'll do my best to explain what I'm after.

I have a table (data) that contains other tables. When data is first created, it can have any number of tables inside it (I expect this to be between 1 and 50).

I want to assign each table to it's own variable.

If I know how many tables there are then this is easy using table1 = data[1]; table2 = data[2] and so on.

I've done a count on the data so that I know the number of entries there are so what I want to do is automatically create the variables, give them a name and assign the corresponding table to it.

So lets say data contains 10 tables. I then want variables created called table1, table2, table3 and so on. table1 should be data[1], table2 should be data[2] and so on.

I'm certain I should create a loop and every time round, have a count=count+1 to create the number attached to the variable.

The problem I have is that I have no idea how to create a variable called 'table'+count (table1).

How do I join the 2 together?

3
  • How is it useful? It will takes 2 times the memory(and hence, memory-inefficient). Commented Aug 22, 2012 at 21:25
  • I'm more curious as to how you plan to use these variables. What problem are you trying to solve that needs this functionality? This sounds like an XY Problem Commented Aug 22, 2012 at 21:36
  • This could very well be an XY Problem. As I said, I'm still learning. Each of the tables contained in data contain the same 5 fields but have different values. I'll don't need most of the data..yet..but at the moment want to add all the values in field 3 in each table. Commented Aug 22, 2012 at 21:50

2 Answers 2

7

The way to create a global variable with a constructed name is to update the global table _G

_G['table'..count] = data[count]

E.g.,

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> count = 3
> _G['table'..count] = 17
> = table3
17
> 
Sign up to request clarification or add additional context in comments.

2 Comments

Though instead of using count (and creating a needless variable), why not do for i = 1,#data do _G['table'..i] = data[i] end? Makes a bit more sense, in my opinion.
To make it more clear perhaps I should have said for count = 1,#data do _G['table'..count] = data[count] end -- but I was only addressing the question of how to make a global variable, not how to make a loop.
-1

You can't, very few programming languages support this and those that do do so through reflection.

The easiest way is to keep your table of tables! All of your tables are stored there already and you can easily refer to a specific table by using data[x] When you do it this was you can refer to your tables using only their index.

1 Comment

Lua is among the languages that allow to create a global variable with an arbitrary name, as is demonstrated above, though it is not the best practice.

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.