Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Outputting all values ??from an array in an array?

Asked by 6 years ago
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

1 answer

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
6 years ago
Edited 6 years ago

So you have an array of arrays. 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
0
Made an edit, there was something wrong. Admittedly I didn't run this code before posting it. XAXA 1569 — 6y
Ad

Answer this question