Lua new table from smaller table

Viewed 45

Struggling with a specific Lua table problem, extracting values from a smaller table to set on/off switches for a longer table.

I have an iterator:

for i = 0,11 do
some things
end

Inside the iterator with length 12 I wish to extract values from a table that might look like this (always <= iterator length):

t = {0,2,4,6,8,10}

and generate a new table with the iterator so that if index i = t value insert 1 else insert 0. That is, the new table would look like this:

newTable = {1,0,1,0,1,0,1,0,1,0,1,0}

Help?

1 Answers
t = {0,2,4,6,8,10}
newTable = {}

for i = 0, 11 do
    newTable[i] = 0
end -- Set the entire new table to 0's

for _, v in ipairs(t) do
    newTable[v] = 1
end -- Loop through t, replace the 0's in new table with a 1 where newTable[v]
Related