Icy North Technologies Forum Index Icy North Technologies
User Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

multi-dimensional table question

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Icy North Technologies Forum Index -> Language
View previous topic :: View next topic  
Author Message
wyn



Joined: 30 Sep 2009
Posts: 3

PostPosted: Wed Sep 30, 2009 12:01 pm    Post subject: multi-dimensional table question Reply with quote

I'm new to lua, but not to coding...which is probably half my problem as after working in php, I'm having difficulties getting my head wrapped around multi-dimensional tables in lua.

Given:

Code:

contacts = { people = {
{ name = "joe", age = 18 }, { name = "pete", age = 21} } }

for l,d in pairs ( contacts["people"] ) do
   print ("(" .. l .. ") " .. d["name"] .. " is " .. d["age"]);
end


Which displays:

Code:

(1) joe is 18
(2) pete is 21


Having exhausted my attempts at table.insert permutations, I'm currently beating my head into the desk trying to figure out how-to dynamically add to the table using table.insert....

I need to use named indices as, later on in the module, I'll be accessing the array content via indexes which are provided from another table.

Help most appreciated. Pointing me to an advanced tutorial on array tables (most of the google'd content deals with simplistic arrays: ary[i][j]=0 ... there seem to be no examples using named indexes as matrices and/or how to EID the data content) would also be a good thing.

thanks!
Back to top
View user's profile Send private message
lhf



Joined: 20 Feb 2006
Posts: 175
Location: Rio de Janeiro, Brazil

PostPosted: Wed Sep 30, 2009 3:07 pm    Post subject: Reply with quote

Doesn't this work?
Code:
table.insert(contacts["people"], {name = "beth", age = 19 })
Back to top
View user's profile Send private message Visit poster's website
wyn



Joined: 30 Sep 2009
Posts: 3

PostPosted: Wed Sep 30, 2009 4:01 pm    Post subject: Reply with quote

It does and did - thank you! The part I wasn't "getting" was that I could build the element list entirely within the function call...I was trying something else entirely.

Another question, sort of general, in regards to this particular example:

given the general declaration for an array table:

Code:

arrayname = { elementname = { { element_item_1 }, ... {element_item_n} } }



The part I personally find misleading is that there are three levels of indexing, denoted by the curly-braces for something which is two-dimensional.

in PHP, I would simple declare something similar as:

Code:

arrayname = array(array());

arrayname[index][element_item_n] = "foo";



lua is personally challenging in this regard - it's that "third level" of indexing responsible for my confusion.

If you, or anyone else, could recommend good references, it'd be much appreciated.

Thank you, again, for your help.
Back to top
View user's profile Send private message
lhf



Joined: 20 Feb 2006
Posts: 175
Location: Rio de Janeiro, Brazil

PostPosted: Wed Sep 30, 2009 4:14 pm    Post subject: Reply with quote

You can write contacts.people[3].name, which is nicer to read.
Back to top
View user's profile Send private message Visit poster's website
wyn



Joined: 30 Sep 2009
Posts: 3

PostPosted: Wed Sep 30, 2009 8:23 pm    Post subject: Reply with quote

final question: Very Happy


When declaring an array, to instantiate the array w/out allocating memory, is it possible to write something like:

Code:
contacts = { people = {{ }}}


And then, later on in the code, during an initialization event, to populate the array either from file, or from the 0th instance of program execution, therein specifying the elements (name, age, etc.)?

Thanks, so much, for your help!
Back to top
View user's profile Send private message
lhf



Joined: 20 Feb 2006
Posts: 175
Location: Rio de Janeiro, Brazil

PostPosted: Wed Sep 30, 2009 8:48 pm    Post subject: Reply with quote

contacts = { people = { } } works fine.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Icy North Technologies Forum Index -> Language All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group