General discussion about LÖVE, Lua, game development, puns, and unicorns.
philnelson
Prole
Posts: 48 Joined: Sun Feb 01, 2009 3:32 am
Post
by philnelson » Wed Feb 04, 2009 11:59 pm
This is a lua question, but I assume it will be of use to new love2d people, also.
In PHP (the language I am most familiar with) I can, for example, create an multidimentional arrays like this:
, and then simply refer to it as
in order to get "a value". How can I accomplish this in lua? I've tried
, which seems to set the value but I cannot then get the value by saying
or similar.
Am I just doing this backwards? Is there a simpler way? I am willing to accept that I'm coming at this the wrong way for this language.
Last edited by
philnelson on Thu Feb 05, 2009 12:09 am, edited 1 time in total.
osgeld
Party member
Posts: 303 Joined: Sun Nov 23, 2008 10:13 pm
Post
by osgeld » Thu Feb 05, 2009 12:07 am
you dont have to set the number when declaring it so
table = {key = "blah"}
is table.key when retrieving it
now if you did
table = {"foo", "bar"}
you would use table[1] or table[2] cause you didnt assign a variable name, which can be mixed, sorta
table = { {key = "foo", value = "bar"}, {item = "gun", ammo = 100}}
the above has 2 unnamed tables, with named contents all in 1 table, so if i wanted to get ammo
table[2].ammo
Last edited by
osgeld on Thu Feb 05, 2009 12:13 am, edited 1 time in total.
philnelson
Prole
Posts: 48 Joined: Sun Feb 01, 2009 3:32 am
Post
by philnelson » Thu Feb 05, 2009 12:10 am
The key is significant to the operation I'm doing. It is a unique ID that is generated elsewhere.
Inny
Party member
Posts: 652 Joined: Fri Jan 30, 2009 3:41 am
Location: New York
Post
by Inny » Thu Feb 05, 2009 3:27 am
osuf oboys
Party member
Posts: 215 Joined: Sun Jan 18, 2009 8:03 pm
Post
by osuf oboys » Thu Feb 05, 2009 4:28 am
philnelson wrote: This is a lua question, but I assume it will be of use to new love2d people, also.
In PHP (the language I am most familiar with) I can, for example, create an multidimentional arrays like this:
, and then simply refer to it as
in order to get "a value". How can I accomplish this in lua? I've tried
, which seems to set the value but I cannot then get the value by saying
or similar.
Am I just doing this backwards? Is there a simpler way? I am willing to accept that I'm coming at this the wrong way for this language.
t[1] = {key='value1'} is correct but is inconsistent with t[1].key, which seems to indicate that there is a single key. If so, then you can use {key, value} as suggested by other posters. If you wish to store multiple values in t[1], then you may iterate the keys and values of t[1] with for k,v in pairs(t[1]).
Last edited by
osuf oboys on Thu Feb 05, 2009 9:16 pm, edited 1 time in total.
bartbes
Sex machine
Posts: 4946 Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:
Post
by bartbes » Thu Feb 05, 2009 6:59 am
philnelson wrote: , which seems to set the value but I cannot then get the value by saying
or similar.
It works? Copied from lua interactive interpreter:
Code: Select all
> t = {}
> t[1] = { key = "value" }
> print(t[1].key)
value
Users browsing this forum: Ahrefs [Bot] and 0 guests