![]() ![]() How many answers are there = #questions - 1 “What year was the Declaration of Independence signed?”, I think that I personally would write my questions and answers with numerical indices, in a way that the question and the correct answer are always in the same place (index), and my app knows that. You may experience strange behaviours -like 0 counts- if your table has string indices (or mixed string and number indices). ![]() to know how many chars are there) and on tables with numerical indices. The lenght operator # works only on strings (ie. However the fastest way to operate on tables with for loops is using numerical indices. Usually nested for loops are performance killers, but sometimes you can't avoid this. However watch out for performance if you need to "abuse" of it also note that pairs outcome may vary its order (somewhere someone made a pairsByKeys lua function to avoid this, but it's even slower because it has to check and order elements before returning them). maybe you won't notice that in this app because you run it only once in a while. That is, what can I use in my code in place of a and b to loop through the number of items in each dimension of the just a couple of notes from my personal experience to integrate SinisterSoft's answer: Is there a way to get the number of elements in each of the dimensions of the table?įor example (a is the number of elements in first dimension and b is the number of elements in the second dimension): But some questions only have 3 choices, not 4, so there’s no way I could do that. If I knew for sure that every question had 5 elements as in the example above, I could just use #questions/5 instead of #questions. If each question has 1 question and 4 answers (i.e, the second index would have possibilities 1-5) #questions would return a number that is 5 times the number of questions in the table. However, I suspect #questions counts all of the elements in the table. If it was a one dimensional array, I would just do for i=1,#questions, with #questions returning the number of questions. That is, I want the loop to do something to the first question, then the same thing to the second and so on. I want to run these questions through a for loop based solely on the first index. Questions = “What year was the Declaration of Independence signed?” The first element of the second index (b) is the text of the question (which is a multiple choice question) and the following elements are the possible answers.įor example (This is for the sixth question in the table.) The first index (a) indicates the number of the question. ![]() I have a 2 dimensional array, questions(a)(b). I’m not sure how to use “#” in a for loop. This is a pretty simple question, but I just don't know the answer. ![]()
0 Comments
Leave a Reply. |