is there a simple way to remove values from a table using another table?
besides looping through both tables to remove individual elements one at a time?
local mainTable = { "apple", "banana", "cherry", "orange", "blueberry" }
local removeTable = { "banana", "orange" }
then the result would be
newTable = { "apple", "cherry", "blueberry" }
i couldnt find anything but so far this
for i, v in pairs(removeTable) do
for j, w in pairs(mainTable) do
if w == v then
table.remove(mainTable, j)
end
end
end
quakage
(quakage)
February 15, 2025, 7:21am
#5
J_Angry:
for i, fruit in pairs(mainTable) do -- loops through mainTable
if table.find(removeTable, fruit) then -- table.find() checks if fruit matches with any values inside of removeTable
table.remove(mainTable, i) -- if true, we remove the fruit from mainTable
end
end
This code won’t work for the following input:
local removeTable = { "banana", "orange", "apple" }
If you want to use table.find you’d need to do something like this:
for i=#mainTable,1,-1 do
if table.find(removeTable, mainTable[i]) then
table.remove(mainTable, i)
end
end
1 Like