Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How can I table.insert() into a table of tables?
Started by ksan May 24 2013 09:02 AM

- - - - -
2 replies to this topic

Best Answer bjsorrentino , 24 May 2013 - 09:31 AM

Hi Kerem,

So you want to insert new tables into "fields", for example going from...

 

 
fields = {    
  {"123"},            
  {"red"},            
  {"apple"}        
}
 
...to this?
 

fields = {    
  {"123"},            
  {"red"},            
  {"apple"},
  {"new1"},
  {"new2"}
}

 

If so, I prefer the index number insertion method over "table.insert()"...in fact, I almost never use table.insert().

 

fields[#fields+1] = {"new1"}
fields[#fields+1] = {"new2"}

 

Brent

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ksan

[GLOBAL: userInfoPane.html]
ksan
  • Corona Geek

  • 2,795 posts
  • Corona SDK

Hi, I have a LUA newbie problem. 

 

I read the LUA tables tutorial at : http://lua-users.org/wiki/TablesTutorial 

 

and read the Corona SDK document on Tables & Arrays at : http://developer.coronalabs.com/content/tables-arrays

 

but still can't do what I need to do. I am hoping a kind soul here can point me in the right direction

 

I have a table of tables defined as : 

 

local fields = {}
 
fields = {    
            {"123"},            
            {"red"},            
            {"apple"}        
        }
 
I can traverse this table using fields[counter][1] approach.
 
numElements = #fields tell me how many table elements there are in my mother table. Life is good so far!
 
Now I want to insert new table elements into fields. So I try 
 
table.insert(fields, [numElements+1][1],  {"test"} )  
 
But it will not budge! Any help into programmatically inserting new table elements into my table will be much much appreciated. 
 
Thank you very much.
 
Regards,
Kerem
  

 

 

 

 



[TOPIC: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

  Best Answer

Hi Kerem,

So you want to insert new tables into "fields", for example going from...

 

 
fields = {    
  {"123"},            
  {"red"},            
  {"apple"}        
}
 
...to this?
 

fields = {    
  {"123"},            
  {"red"},            
  {"apple"},
  {"new1"},
  {"new2"}
}

 

If so, I prefer the index number insertion method over "table.insert()"...in fact, I almost never use table.insert().

 

fields[#fields+1] = {"new1"}
fields[#fields+1] = {"new2"}

 

Brent



[TOPIC: post.html]
#3

ksan

[GLOBAL: userInfoPane.html]
ksan
  • Corona Geek

  • 2,795 posts
  • Corona SDK

Hi Brent, thank you so very much!!! I appreciate your quick response. I will try this right away.

 

Edit - Worked like a charm!!! Thank you very much.




[topic_controls]
 
[/topic_controls]