Help With Table Logic

I am confused and in dire need of some help on this.

I know that in LUA properties are dynamic and can be added to just about any object at any time.

So I creating a Quiz Table and trying to do the following assignment.
But I get an error “attempt to index field ‘?’”
Can’t we add properties to Table Items?

[lua]local myTable = {}
for loopCount=1,7 do
myTable[loopCount].myID = loopCount
end[/lua]

I know I can do this and this works fine. But I would have expected the above code to also work.
I would have preferred not to have to create a bunch of items as in below then pull them out of the table but I can if needed.

[lua]local myTable = {}
local myItem
for loopCount=1,7 do
myItem.myID = loopCount
myTable[loopCount] = myItem
end[/lua]

Thanks
Larry
DoubleSlashDesign.com [import]uid: 11860 topic_id: 12681 reply_id: 312681[/import]

I am not 100% sure what you are trying to do, but I will give it a crack. If you say

local myTable

you create a variable with value nil. If you say

local myTable = {}

you create an empty table, and set myTable to reference it.
if you say

myTable.gack = “Ack”

you are creating a table element named gack with the value “Ack”

In the loop, if you say

myTable[loopCount] = loopCount

you creating an element whose name is the value of loopCount, and whose value is the value of loopCount. The [] treats the table like an array. If you say

myTable[loopCount].myID = loopCount

when loopCount is 1, you are telling it to set element myID of element 1 of myTable to loopCount. Or myTable.1.myID Since that does not exist, you get an error.

I think what you want is an array(table) of tables. Take a look at this (I put in a simple table printer to help show what is going on.)

---------------------------------------  
function printTable( t ) -- Table ref  
  
 print("-- "..tostring(t).." --")  
 for k,v in pairs(t) do  
 print("ELEMENT "..tostring(k).." VALUE "..tostring(v).." TYPE IS "..type(v))  
 end  
  
 print("---------------------")  
end  
  
local myTable = {}  
myTable.gack = "Ack"  
  
for loopCount=1,4 do  
 --myTable[loopCount].myID = loopCount -- nil field value  
  
 --myTable[loopCount] = loopCount -- Value = index  
  
 -- Table of tables --  
 myTable[loopCount] = {} -- New tabled referenced by myTable[loopCount]  
 myTable[loopCount].myID = loopCount  
 myTable[loopCount].myIDPlusOne = loopCount + 1  
end  
  
printTable(myTable)  
printTable(myTable[1])  
  

You should see something like this

– table: 02E79E48 –
ELEMENT 1 VALUE table: 02E79E98 TYPE IS table
ELEMENT 2 VALUE table: 02E79E70 TYPE IS table
ELEMENT 3 VALUE table: 02E79EC0 TYPE IS table
ELEMENT 4 VALUE table: 02E79F10 TYPE IS table
ELEMENT gack VALUE Ack TYPE IS string

– table: 02E79E98 –
ELEMENT myIDPlusOne VALUE 2 TYPE IS number
ELEMENT myID VALUE 1 TYPE IS number

[import]uid: 41667 topic_id: 12681 reply_id: 49269[/import]

not an expert in Lua, but I think the problem is you are trying to add attributes to something that hasn’t yet been defined/created.

also not in front of my coding computer, but I think this will work…

local myTable = {}
for loopCount=1,7 do
myTable[loopCount] = {} – or just define myTable[loopCount] as something, a display object etc…
– then add the attribute
myTable[loopCount].myID = loopCount
end [import]uid: 49842 topic_id: 12681 reply_id: 49271[/import]