How To Get Around Declaring The Internal Structure Of A Table?

I’m building a multiple choice test/quz app that needs to keep a record of answers.

I have a table that stores data for each question answered.

results = {

{answer = {}, correct, attempts},

{answer = {}, correct, attempts},

}

and I add data like

q = questionIndex

i = answer

results[q].answer[a] = results[q].answer[a] + 1

this works but but I’m planning to have 300+ to unlimited questions, so with my code above I need to copy the empty declaration {answer = {}, correct, attempts} 300 plus times in my source !

Is there a way around this?

The only solution I can think of is ‘losing’ the structure so it becomes a ‘flat array’ like results = {} and then just indexing the correct number of records to store the data.

What is the magic Lua solution? once I have the data on the App I plan to keep an online database of all users of the app

I believe table.insert is what you need to populate the empty table into each index:
 

local numResults = 300 results = {} for i = 1, numResults do local tmpTable = {answer={}, correct,attempts} table.insert(results, tmpTable) end

I didn’t even tried the code above, but should work if i remember correctly.

I believe table.insert is what you need to populate the empty table into each index:
 

local numResults = 300 results = {} for i = 1, numResults do local tmpTable = {answer={}, correct,attempts} table.insert(results, tmpTable) end

I didn’t even tried the code above, but should work if i remember correctly.