Best strategy to check all elements of a table

4 messages
Open this post in threaded view
|

Best strategy to check all elements of a table

 Hi! I'm testing porting some C++ code to Lua. One part of the code checks all elements on an array against each other. Is there some optimized way of do this in Lua? I don't have to check collisions twice, only one time for each pair of objects. In pseudocode: for obj1 = list.first to list.end do    for obj2 = list.first to list.end do           if obj1.collideWith(obj2) then         obj1.DoSomething(obj2)       end    end end Thanks for any tip! __________________________________________________ Faça ligações para outros computadores com o novo Yahoo! Messenger http://br.beta.messenger.yahoo.com/
Open this post in threaded view
|

Re: Best strategy to check all elements of a table

 On Mon, May 29, 2006 at 06:57:58PM +0000, Jose Marin wrote: > I don't have to check collisions twice, only one time > for each pair of objects. > > In pseudocode: > > for obj1 = list.first to list.end do >    for obj2 = list.first to list.end do >     >       if obj1.collideWith(obj2) then >         obj1.DoSomething(obj2) >       end >    end > end More of an algorithm question than Lua, but you probably want (using your pseudocode): for obj1 = list.first to list.end do    for obj2 = obj1+1 to list.end do       if obj1.collideWith(obj2) then         obj1.DoSomething(obj2)       end    end end or in Lua: list = { "a", "b", "c", "d", "e" } for k, v in next, list, nil do    -- same as pairs(list)   for k2, v2 in next, list, k do  -- like pairs(list), but start at next(k)     print(v .. ", " .. v2);   end end -- Glenn Maynard