How can I table.insert() into a table of tables?

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

 

Hi Kerem,

So you want to insert new tables into “fields”, for example going from…

[lua]

fields = {    

  {“123”},            

  {“red”},            

  {“apple”}        

}

[/lua]

…to this?

[lua]

fields = {    

  {“123”},            

  {“red”},            

  {“apple”},

  {“new1”},

  {“new2”}

}

[/lua]

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

[lua]

fields[#fields+1] = {“new1”}

fields[#fields+1] = {“new2”}

[/lua]

Brent

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.

Hi Kerem,

So you want to insert new tables into “fields”, for example going from…

[lua]

fields = {    

  {“123”},            

  {“red”},            

  {“apple”}        

}

[/lua]

…to this?

[lua]

fields = {    

  {“123”},            

  {“red”},            

  {“apple”},

  {“new1”},

  {“new2”}

}

[/lua]

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

[lua]

fields[#fields+1] = {“new1”}

fields[#fields+1] = {“new2”}

[/lua]

Brent

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.