Table question

Hi ive noticed something new with tables that i want to better understand, i can see it has its use.

 layout = { ["alphaBTN"] = {100,100}, ["blueBTN"] = 2, ["bottomBar"] = 3, ["greenBTN"] = 4, ["hexBTN"] = 5, ["infodisplay"] = 6, ["palleteBTN-14"] = 7, ["palleteBTN"] = 8, ["pallettholder"] = 9, ["playBTN"] = 10, ["redBTN"] = 11, ["rightBar"] = 12, ["solidBTN"] = 13, ["topBar"] = 14, } print(#layout) -- will output zero ????????? print(layout["alphaBTN"][1]) -- will output 100, great :) print(layout[1][1]) -- will cause error and not find anything

I want to be able to use the above structure in the table as Texture Packer outputs and it will save time. 

Is there a way I can cycle through the table with a for loop, i dont know how this could be done in a nice dynamic way without knowing the amount of fields/items in the table. 

Anyone able to enlighten me on whats happening? Thx. 

Yes, you can use the ‘pairs’ iterator, which is a Lua feature that lets you loop through all the elements in a table.  Check it out here: http://www.lua.org/pil/7.3.html

If you haven’t already, it’s worth reading that entire book.

  • Andrew

DA DARR. Thats magic thx very much. im so used to doing the better way, i neve really touch ipairs or pairs but that was very helpful. Thanks you. 

for k, v in next, layout do print(k, v) end --does everything --rightBar 12 --redBTN 11 --blueBTN 2 --playBTN 10 --pallettholder 9 --palleteBTN 8 --infodisplay 6

Yes, you can use the ‘pairs’ iterator, which is a Lua feature that lets you loop through all the elements in a table.  Check it out here: http://www.lua.org/pil/7.3.html

If you haven’t already, it’s worth reading that entire book.

  • Andrew

DA DARR. Thats magic thx very much. im so used to doing the better way, i neve really touch ipairs or pairs but that was very helpful. Thanks you. 

for k, v in next, layout do print(k, v) end --does everything --rightBar 12 --redBTN 11 --blueBTN 2 --playBTN 10 --pallettholder 9 --palleteBTN 8 --infodisplay 6