lua-users home
lua-l archive

LeftJoins with tables (removing values from tables)

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


Hello,
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 
them. 
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
 end
 end
 return tmp
end
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 
remove. 
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)
 end
end
Thanks
-- 
Clemens Wacha wacha@gmx.ch ICQ:12620942
successfully using Linux since 1999. See http://www.debian.org

AltStyle によって変換されたページ (->オリジナル) /