lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Very often I have collection of objects and I want to know which 
objects from both collections are in common or are only in one of 

For this I have created the following pseudo code function that 
subtracts one table from the other

-- subtracts all elements in table2 from table1
function( table1, table2)
 tmp = copy of table1
 for all objects in table1 do
  if there is one too in table2
   remove it from tmp
 return tmp

In lua I made a deep copy of the table and tried to achieve this 
using table.remove but this function sucks somehow. Since i am 
removing elements from a table the index changes inside the table 
and this prevents me from _efficiently_ finding the objects to 

Now my question: Can I travel a table using for in Lua AND remove 
objects form the same table inside the for loop?

for key, value in table do
 if value == "object_to_delete" then
  table.remove(table, key)

Clemens Wacha               ICQ:12620942

successfully using Linux since 1999. See