S O M E O N E H E L P
I think this is a BUG
At first I thought this was a table issue but I placed a print command in the for each loop and the data elements actually were read in, in an incorrect order.
Example
First I have a module called questions it hold a table and function called getTrvia as shown below.
I actually have 10 questions but only placed 2 in here as an example
[lua]function getTrivia()
print(“this is getTrivia”)
local questionData = {}
questionData.quesionCount=10
questionData.objects =
{
q1 =
{
qid=1,
question = “This is Question 1”,
option1 = “This is Option 1”,
option2 = “This is Option 2”,
option3 = “This is Option 3”,
option4 = “This is Option 4”,
answer = 2
},
q2 =
{
qid=2,
question = “This is Question 2”,
option1 = “This is Option 1”,
option2 = “This is Option 2”,
option3 = “This is Option 3”,
option4 = “This is Option 4”,
answer = 4
},
}
–Etc… all the way to 10
return questionData
end[/lua]
now in my main.lua file I read the data from the trivia and place them in a table to work with like so
[lua]local loopCount = 0
local tblTrivia = {}
for key,data in pairs(questionsData.objects) do
loopCount = loopCount + 1
local myTrivia = {}
– print(data.question) – put this in and watch them print out of order
myTrivia.qid = data.qid
myTrivia.question = data.question
myTrivia.option1 = data.option1
myTrivia.option2 = data.option2
myTrivia.option3 = data.option3
myTrivia.option4 = data.option4
myTrivia.answer = data.answer
myTrivia.userAnswer = 0 --Has not yet been asked
table.insert(tblTrivia, loopCount, myTrivia)
end
–Try two different ways to print
print("table size is " … #tblTrivia)
myTrivia = tblTrivia[1]
print("this is QID = " … myTrivia.qid)
myTrivia = tblTrivia[2]
print("this is QID = " … myTrivia.qid)
print("QID = " … tblTrivia[3].qid)
print("QID = " … tblTrivia[4].qid)
print("QID = " … tblTrivia[5].qid)
print("QID = " … tblTrivia[6].qid)
print("QID = " … tblTrivia[7].qid)
print("QID = " … tblTrivia[8].qid)
print("QID = " … tblTrivia[9].qid)
print("QID = " … tblTrivia[10].qid)[/lua]
Results from code above:
table size is 10
this is QID = 9
this is QID = 8
QID = 4
QID = 5
QID = 6
QID = 7
QID = 3
QID = 2
QID = 1
QID = 10
As you can see the items are read out of order in the foreach loop.
Why is this and is there a way to fix it?
Items should have been read in from the for each loop in a direct 1-10 order.
[import]uid: 11860 topic_id: 12747 reply_id: 312747[/import]