This is a pretty simple question, but I just don't know the answer. I’m not sure how to use “#” in a for loop.
I have a 2 dimensional array, questions(a)(b). The first index (a) indicates the number of the question. 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.
For example (This is for the sixth question in the table.)
questions [question] = “What year was the Declaration of Independence signed?”
questions [correctAnswer] = “1776”
questions [wrongAnswer1] = “1917”
questions [wrongAnswer2] = “1812”
questions [wrongAnswer3] = “1865”
questions [wrongAnswer4] = “1945”
I want to run these questions through a for loop based solely on the first index. That is, I want the loop to do something to the first question, then the same thing to the second and so on. If it was a one dimensional array, I would just do for i=1,#questions, with #questions returning the number of questions. However, I suspect #questions counts all of the elements in the table. 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. Is this correct?
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. But some questions only have 3 choices, not 4, so there’s no way I could do that.
Is there a way to get the number of elements in each of the dimensions of the table?
For example (a is the number of elements in first dimension and b is the number of elements in the second dimension):
for i=1,a do
for j=1,b do
code to be performed
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 table?