Page 1 of 2

Question for tables and variables :3

Posted: Mon Apr 14, 2014 1:42 pm
by Volgoza
Hello.

Someone help me.
Can i use variable for function table.insert?

Example:

Code: Select all

Table = {}

SomeTable = {}
SomeTable.var = "Table"

function love.mousepressed(x, y, button)
if button == "l" then
table.insert (SomeTable.var, 1)
end
end
That is to substitute the variable as the table name?

Sorry for my english :3 And ty for answer.

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 1:45 pm
by Snuux
In your example, SomeTable.var has String type.
table.insert (SomeTable.var, 1) - first parameter must be Table - not String...

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 2:26 pm
by Robin
You need to change one line:

Code: Select all

SomeTable.var = Table

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 2:34 pm
by Volgoza
Then it will be and if the variable assignment table does not exist, then do not get anything. I already tried.
Or I did not understand you.

But i did:

https://love2d.org/imgmirrur/ykNXPAr.jpg

where vv.name its - BigCrystal or SmallCrystal1 or SmallCrystal2.

Understand or no?

If you know, how i can get it easy, i would appreciate :3

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 3:23 pm
by Robin
Volgoza wrote:If you know, how i can get it easy, i would appreciate :3
I'm afraid I don't understand what you're saying.

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 3:32 pm
by Ranguna259
What's your native language ?

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 4:13 pm
by Snuux
His native language - is Russian. I told him, that it's code is little strange...

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 6:22 pm
by zorg
i could be wrong, but maybe Volgoza wants this:

Code: Select all

table.insert(someTable[var], 1)
Basically, if you do the dot syntax like you did before, it will interpret that as the string "var", or more visually, as:

Code: Select all

someTable['var'] -- is the same as: someTable.var
Note that this is how it works everywhere, not just with table.insert.

Edit: I still think my version is what he wants, though he needs to do

Code: Select all

SomeTable.var = Table
instead, without the quotes around Table.

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 7:01 pm
by Ranguna259
OOH now I see what he wants :crazy: (maybe)
I think he wants to add something to the "Table" table throught the "SomeTable" table, like so:

Code: Select all

table1 = {}
table2={table1}

table2[1].a = 1
--and so:
table1.a == 1
A pointer of some sort ?
Well I think that that is impossible, correct me if I'm wrong but when you create a table using another table you'll just be creating another table that is completly isolated from any other table. (table, table, table, table, table,)

Re: Question for tables and variables :3

Posted: Mon Apr 14, 2014 9:55 pm
by moikmellah
Perhaps something like this is what the OP is looking for?

Code: Select all

Table = {}

SomeTable = {}
SomeTable.var = "Table"

function love.mousepressed(x, y, button)
  if button == "l" then
    local myTable = _G[SomeTable.var]
    table.insert (myTable, 1)
  end
end
That allows you to reference a variable by name through the global table _G (scope allowing, of course). Whether it's a wise idea is up for debate, but I think that's what the poster is going for.

(source: PIL 14.1)