I need to interact with an array and delete some elements.
But it seems table.remove
shifts the Id and it’s messing the for / ipairs loop:
local arr = {}
for i = 1, 10, 1 do -- will create arr{"value1" ... "value10"}
table.insert(arr, "value" .. i)
end
for i, v in ipairs(arr) do
print(i, v)
table.remove(arr, i)
end
This will print:
1 value1
2 value3
3 value5
4 value7
5 value9
and the array will remain with:
"value2", "value4", "value6", "value8", "value10"
What’s the correct way to interact with an array and remove items inside a loop?