Table insert name

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
soytak111
Prole
Posts: 25
Joined: Sat Sep 17, 2022 1:00 am

Table insert name

Post 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
lua,contrary to other language,give you as much space as moon :awesome:
User avatar
dusoft
Party member
Posts: 676
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Table insert name

Post by dusoft »

This is basic Lua. Did you search before posting?

Code: Select all

 
basic="search" 
tutorials={} 
tutorials[basic]=1
print (tutorials.search)
soytak111
Prole
Posts: 25
Joined: Sat Sep 17, 2022 1:00 am

Re: Table insert name

Post 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
lua,contrary to other language,give you as much space as moon :awesome:
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Table insert name

Post by togFox »

Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
dusoft
Party member
Posts: 676
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Table insert name

Post 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.
User avatar
darkfrei
Party member
Posts: 1209
Joined: Sat Feb 08, 2020 11:09 pm

Re: Table insert name

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
soytak111
Prole
Posts: 25
Joined: Sat Sep 17, 2022 1:00 am

Re: Table insert name

Post 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:
lua,contrary to other language,give you as much space as moon :awesome:
User avatar
darkfrei
Party member
Posts: 1209
Joined: Sat Feb 08, 2020 11:09 pm

Re: Table insert name

Post by darkfrei »

Right:

Code: Select all

tutorials[basic] = 1
Wrong:

Code: Select all

tutorials.[basic] = 1
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Giapp, Google [Bot] and 5 guests