Does a for loop's index change when deleting a value from a table?

Solution: Do not use a non-numeric for loop to remove objects from a table.

I am trying to remove all traces of an inserted value in a table. I know how to do this using a numeric for loop, but I am curious as its usage in non-numeric for loops.

Let’s say that a value “exampleValue” was inserted multiple into a table “t”, along with arbitrary values, using the following code:

table.insert(t, exampleValue)

Now let’s say that this table has multiple values, and two instances of the value “exampleValue” exist. Is the necessity of an indexOffset necessary? For instance:

local exampleValue = {"Example"} -- Example value used above
local indexOffset = 0

for index, value in next, t do
	if value == exampleValue then
		table.remove(t, index - indexOffset)
		indexOffset += 1
	end
end

Or, does a for loop created using in next, table do this automatically?

1 Like

A for loop created using in next, table automatically accounts for table alterations. However, in the event that an object is removed from the table, it will skip over any objects moved down, and will continue to increase the index by 1.

Do not use a non-numeric for loop to remove objects from a table. If anyone has anything to add, I will accept your solution instead of my own.

Just a note for you that while the += operator might work in other languages such as Python it doesn’t work in Lua so you have to do indexOffset = indexOffset + 1 to do this, here’s what the error will roughly look like if you try to use the += operator:

However if you assign it to itself and add the number that you want to add to it then it will work just fine as shown in the image below:

Hope this helped you and gave you a better idea of how Lua works. :slightly_smiling_face:

I appreciate the response, and this was the case in the past, but Roblox recently added support for various compound assignments in Update 435. Roblox is really helping us devs out! :smiley:

That being said, what you said is correct for standard Lua.

2 Likes

“Does a for loop’s index change when deleting a value from a table?”
It doesn’t, as you’ve just learned. table.remove will shift left all table elements after the removed one to fill the gap.

There are other ways to do this with arrays (not necessarily better ones)

1:
Traverse the table backwards with a numeric loop.

for index = #t, 1, -1 do
	if t[index] == exampleValue then
		table.remove(t, index)
	end
end

table.remove does nothing to array elements that are before the one removed.

2:
Make a table (array). Instead of table.remove in your original code, insert the value into the new table. Return the new table. This is not suitable if the original table is reused elsewhere, as it will continue to have the supposedly removed items.

3:
Whenever you remove an element in your original code, try again without changing index.

for index, value in next, t do
	while value == exampleValue do
		table.remove(t, index)
		value = t[index] -- important
	end
end

This is very goofy, but if you’re bent on using a non-numeric for loop, this is the way to go.

2 Likes