Page 1 of 1

Table insert name

Posted: Wed Oct 18, 2023 2:39 pm
by soytak111
Table = {}
Name = "some"
Table.? = "something"

My goal is that at Table.some store "something"
I tried that
Table.(Name) = "something"
But it still doesn't worked

Re: Table insert name

Posted: Wed Oct 18, 2023 5:41 pm
by dusoft
This is basic Lua. Did you search before posting?

Code: Select all

 
basic="search" 
tutorials={} 
tutorials[basic]=1
print (tutorials.search)

Re: Table insert name

Posted: Wed Oct 18, 2023 6:45 pm
by soytak111
dusoft wrote: Wed Oct 18, 2023 5:41 pm This is basic Lua. Did you search before posting?

Code: Select all

 
basic="search" 
tutorials={} 
tutorials[basic]=1
print (tutorials.search)
Doesn't seem to quite work
When i try to print,it give me an error(nil value)like it actually don't put any value in the table

Re: Table insert name

Posted: Wed Oct 18, 2023 10:29 pm
by togFox

Re: Table insert name

Posted: Wed Oct 18, 2023 10:58 pm
by dusoft
soytak111 wrote: Wed Oct 18, 2023 6:45 pm
dusoft wrote: Wed Oct 18, 2023 5:41 pm This is basic Lua. Did you search before posting?

Code: Select all

 
basic="search" 
tutorials={} 
tutorials[basic]=1
print (tutorials.search)
Doesn't seem to quite work
When i try to print,it give me an error(nil value)like it actually don't put any value in the table
It prints 1 correctly. Check your setup.

Re: Table insert name

Posted: Thu Oct 19, 2023 7:10 am
by darkfrei
Let's make a function for it!

Code: Select all

local function insertKeyValue (tabl, key, value)
  tabl[key] = value
end
Now you can use it as

Code: Select all

tutorials={}
basic = "search"
n = 1
insertKeyValue (tutorials, basic, n)

print(tutorials.search) -- prints 1

Re: Table insert name

Posted: Thu Oct 19, 2023 12:10 pm
by soytak111
darkfrei wrote: Thu Oct 19, 2023 7:10 am Let's make a function for it!

Code: Select all

local function insertKeyValue (tabl, key, value)
  tabl[key] = value
end
Now you can use it as

Code: Select all

tutorials={}
basic = "search"
n = 1
insertKeyValue (tutorials, basic, n)

print(tutorials.search) -- prints 1
That work! Its weird that it don't work in code but that the function work :awesome:

Re: Table insert name

Posted: Thu Oct 19, 2023 12:43 pm
by darkfrei
Right:

Code: Select all

tutorials[basic] = 1
Wrong:

Code: Select all

tutorials.[basic] = 1