Table.remove() not working properly

I’ve been attempting to get this to work for days. I have this code:

for i,Position in ipairs(Adjacents) do
	if FindCubeBasedOnPosition(Position) then
		RemoveIndexes[#RemoveIndexes+1] = i
	end
end
	
print(#RemoveIndexes)
	
for i,v in ipairs(RemoveIndexes) do
	table.remove(Adjacents,v)
end
	
print(#Adjacents)

What this code does is that it takes in a table, a table of, let’s say five (in this case) spots. It iterates through these spots, and checks if that spot is already occupied. If so, then it adds that position to a table. Again, for this example, let’s say that 4 of the 5 spots are occupied. It correctly puts those 4 spots in the table, and when printing #RemoveIndexes it will correctly print out 4.

Alright, everything’s working fine. We then iterate through the RemoveIndexes table, and call table.remove on each index that is in there. Okay, so when we print #Adjacents there should be only one left, right?

NO

I am completely baffled. In my many years of programming on this platform never once have I had this issue. Not once. Please, explain to me what is going on here as I am utterly stumped.

I have run numerous checks, the proper values are stored in RemoveIndexes, which in this case are the values stored at 2, 3, 4, and 5. If I were to replace table.remove with just simply setting the value to nil, it works, however this will break code later on.

I have the same issue with the similar code:

for i,Position in ipairs(Adjacents) do
	if FindCubeBasedOnPosition(Position) then
		table.remove(Adjacents,i)
	end
end

print(#Adjacents)

I think

for i,v in ipairs(RemoveIndexes) do
table.remove(Adjacents,v)
end

replace “v” with “i”
or
replace “v” with Adjacents[v]

I don’t know if it works but you can try

1 Like

yea, table.remove expects the index (number) so it should be i

1 Like

Here’s another problem: table.remove() shifts the table when you remove an element, so it’s bad practice to use it while iterating. Instead, try setting it to nil manually or iterate through the table backwards.

edit: nevermind, I didn’t notice that you were removing it from a seperate table.

5 Likes

You see, I do feed table.remove a number. The table RemoveIndexes stores the indexes of the values that are supposed to be removed. I then feed those numbers into table.remove. This entire convoluted mess of code was started because the code I posted at the bottom of my post didn’t work either.

I also said setting to nil was not an option as that’d break my code. Iterating backwards? I’ll see.

What do you mean by “occupied?” if it’s a nil value, then you should use pairs for this since ipairs stops at a nil value.

Alright, let me draw you something.


The game consists of a large 3D grid of cubes. For a space to be occupied, it means there is already a cube there. If I fill a value with nil rather than using table.remove, the table will still have a value stored there, so even if the table is:

{1,nil,nil,14,3952394234,nil}

the #Table is 6, rather than 3. This will end up breaking my code later down the line with nil values, and can lead to certain memory issues.

Currently, using the reverse iteration seems to have worked. I’ll mark your reply as an answer after I finish bugtesting.

can you just skip the extra loop/adding to a table and directly remove from the adjacent spots (while iterating backwards)?

Yup, the entire extra loop/table was because I mistakenly believed ipairs was iterating through the updated table. I was partially correct, I guess. I use this code now:

for i = #Adjacents,1,-1 do
	if FindOreBasedOnPosition(Adjacents[i]) then
		table.remove(Adjacents,i)
	end
end
1 Like