How could I remove any specific elements from a table/array?

I am trying to remove elements from a table. For example, let’s say this is my table.

local table = {"Apple","Apple","Orange","Apple","Apple","Apple","Banana","Apple","Apple","Pear","Apple"}

I want to remove all the "Apple"s so the table looks like this:

local table = {"Orange","Banana","Pear"}

How do I achieve this? Thanks in advance.

1 Like
table.remove(table,table.find(table,"Apple"))
1 Like

The table was unchanged when I printed it.

Try this

local Tab = {"Apple","Apple","Orange","Apple","Apple","Apple","Banana","Apple","Apple","Pear","Apple"}

while table.find(Tab,"Apple") do
	table.remove(Tab,table.find(Tab,"Apple"))
end

print(Tab)
local mytab = {"Apple","Banana","Apple","Carrot","Banana","Apple","Melon"}


for index,value in pairs(mytab) do
	if value:match("Apple") then
		table.remove(mytab,index)
	end
end


for _,v in pairs(mytab) do
	print(v)
end

Result:

--Banana , Carrot ,Banana ,Melon

Unfortunately since my table is over 100 elements long, I have to do that function multiple times for it to work. But when I do that it works like a charm. Thanks!

I would probably do this as it won’t iterate through the entire array every time (technically it would, but not in Lua). But both are probably fine as long as this isn’t happening rapidly and constantly.

Not sure how, but it doesn’t work for me.

It works fine for me:

image

It may be because my table is from :GetTouchingParts().

Ah well yes. GetTouchingParts will return parts and not strings. The other solution will have to do then.