-BUG - CORONA - FOR EACH not working correctly

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]

Your “questionData.objects” is indexed via objects rather than just numbers, meaning the order isn’t set in stone, try defining it like this instead:

  
 questionData.objects =  
 {  
 {  
 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  
 },  
 {  
 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  
 },  
 }  
  

Not sure if that is correct, but basically you probably need to use an indexed array. Or might be able to sort the table after you have initialised the questions, something like this:

table.sort( questionData.objects, function( a,b ) return a.qid \< b.qid end) [import]uid: 5833 topic_id: 12747 reply_id: 46744[/import]

Very intereting… Thanks for the info.

I still think it should be traversed as its is in the table def.

I changed your sort like this

table.sort( tblTrivia, function( a,b ) return a.qid < b.qid end)

And it worked perfect… Thanks so much.

Larry [import]uid: 11860 topic_id: 12747 reply_id: 46745[/import]

try this
[lua]local questionData =
{{qid=1,question = “Q1”},
{qid=2,question = “Q2”},
{qid=3,question = “Q3”},
{qid=4,question = “Q4”},
{qid=5,question = “Q5”},
{qid=6,question = “Q6”},
{qid=7,question = “Q7”},
{qid=8,question = “Q8”},
{qid=9,question = “Q9”},
{qid=10,question = “Q10”},}

for key,data in pairs(questionData) do
print("key : “…key…” qid : "…data.qid)
end
[lua] [import]uid: 71210 topic_id: 12747 reply_id: 46746[/import]

Tables can be fiddly but at least you got it working. Can always try to make it work differently/better at a later date if needed but for now it should do. [import]uid: 5833 topic_id: 12747 reply_id: 47180[/import]

.
A foreach loop on a table that is set up like an object :

for key,val in pairs(myTable) do print("key: " .. key) print("val: " .. val) end
A foreach loop on a table that is set up like an array :

for i,val in ipairs(myTable) do print("i: " .. i) print("val: " .. val) end

The difference is between iPAIRS and PAIRS (take a closer look again)

ipairs = indexed  
  
pairs = key::value  

NOTE: The :: doesn’t mean anything other than it was a cool way to express the relationship between the key and value.

To get real tricky on key::values you can use:

for key, val in next, myTable do  
 print("key: " .. key)  
 print("val: " .. val)  
end  

Where “next” is a LUA keyword which accesses the internal operation of the pairs() function within the LUA language.

See:

http://lua-users.org/wiki/TablesTutorial
http://www.lua.org/pil/7.3.html
[import]uid: 616 topic_id: 12747 reply_id: 73065[/import]

Lua simply doesn’t provide a native mechanism to access non-numerically-indexed tables in the order in which they were defined. For more information:

http://lua-users.org/wiki/ForTutorial

The pairs() function will allow iteration over key-value pairs. Note that the order that items are returned is not defined, not even for indexed tables.

As with pairs(), the order in which items are returned is not defined; index keys can be returned in any order, not just numerically increasing.

http://www.lua.org/pil/7.3.html

The pairs function, which iterates over all elements in a table, is similar, except that the iterator function is the next function, which is a primitive function in Lua. The call next(t, k), where k is a key of the table t, returns a next key in the table, in an arbitrary order. [import]uid: 71767 topic_id: 12747 reply_id: 73267[/import]