so heres the code in question
[code]
for tqid, tqv in pairs(Config.TestQuestions) do
for aid, av in pairs(answers) do
if tqv.question == av.question and tqv.correct != av.answer then
failed = true
end
end
end
[/code]
As you can see i have a for loop running inside another for loop (i know it sounds bad already) but im not sure theres another way to do what im trying to do...
I have a table in my config
[code]
config.TestQuestions = { -- The questions
{
question = "Test question 1",
options = {"test answer 1", "test answer 2", "test answer 3", "test answer 4"},
correct = "test answer 2"
},
{
question = "Test question 2",
options = {"test answer 1", "test answer 2", "test answer 3", "test answer 4"},
correct = "test answer 2"
},
{
question = "Test question 3",
options = {"test answer 1", "test answer 2", "test answer 3", "test answer 4"},
correct = "test answer 2"
},
}
[/code]
Now this table is only seen serverside NOT clientside because then the client could see the correct answer... So send a modified table to the client with only the questions and the options... Then when they are done with the questions i create a table clientside to send to the serverside with the question and the answer they gave
[code]
QuestionOptions.OnSelect = function( panel, index, value )
for paid, pa in pairs(plysanswer) do
if pa.question == v.question then
pa.answer = value
end
end
end
-- yes i know i have for paid and pa however im using this for loop inside of a greater for loop that uses k, v (for the questions on my derma)
[/code]
So now when this is all sent to the server side i have that code at the top (first code block)
Now my idea is that if i loop through the servers config questions and then i loop through the answers given and compare them i can see if they match however the double for statement does not look right.. I tried using the whole table.HasValue however it seems like that would not make sense in this case...
Thanks for reading any help would be great!
Sometimes you will require it, it doesn't mean it's something bad
i don't find myself using it often in glua, but w/e dealing with json data you will almost always find yourself looping through loops.
Thanks guys for the answer! Seems like the overall answer is it is alright to have a loop inside of a loop! Ill leave this open in case anyone wants to add!
If you are able to do something without a second loop aka increasing the big O efficiency, do it, but some things are going to require doubly or triply-nested for loops.
Sorry, you need to Log In to post a reply to this thread.