local array = {} array[1] = {1,2,3,4,5} array[2] = {5,4,3,2,1} for i,v in pairs(array) do print(array[i][i]) end
This code outputs to output only 1 and 4. how do I output first the numbers from the first array (1,2,3,4,5) and then from the second, from the third one and so on
So you have an array
of array
s. To print each entry, you need two nested for-loops. That is, a for-loop inside a for-loop.
Also, since you want to print the value of each entry, not its key (also known as its index), you would want to print v
instead of i
.
local arrays = {} array[1] = {1, 2, 3, 4, 5} array[1] = {5, 4, 3, 2, 1} -- Iterate through each array in -- the list of arrays. -- For clarity, we replace i with an underscore (_) -- since we won't be using it anyway. for _, array in pairs(arrays) do -- Iterate through the array and print -- its value. for _, v in pairs(array) do print(v) end end