lua tables

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
philnelson
Prole
Posts: 48
Joined: Sun Feb 01, 2009 3:32 am

lua tables

Post by philnelson »

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:

Code: Select all

array[1]['field'] = "a value"
, and then simply refer to it as

Code: Select all

array[1]['field']
in order to get "a value". How can I accomplish this in lua? I've tried

Code: Select all

t[1] = {key='value'}
, which seems to set the value but I cannot then get the value by saying

Code: Select all

t[1].key
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.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: lua tables

Post by osgeld »

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

Re: lua tables

Post by philnelson »

The key is significant to the operation I'm doing. It is a unique ID that is generated elsewhere.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: lua tables

Post by osgeld »

i added alot since you posted :)

and heres the big book of plain lua

http://www.lua.org/manual/5.1/
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: lua tables

Post by Inny »

Code: Select all

t = {}
t[1]={}
t[1].key = value
or

Code: Select all

t = { {["key"]=value}
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: lua tables

Post by osuf oboys »

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:

Code: Select all

array[1]['field'] = "a value"
, and then simply refer to it as

Code: Select all

array[1]['field']
in order to get "a value". How can I accomplish this in lua? I've tried

Code: Select all

t[1] = {key='value'}
, which seems to set the value but I cannot then get the value by saying

Code: Select all

t[1].key
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.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: lua tables

Post by bartbes »

philnelson wrote:

Code: Select all

t[1] = {key='value'}
, which seems to set the value but I cannot then get the value by saying

Code: Select all

t[1].key
or similar.
It works? Copied from lua interactive interpreter:

Code: Select all

> t = {}
> t[1] = { key = "value" }
> print(t[1].key)
value
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests