lua-users home
lua-l archive

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


If iteration order is unimportant, I iterate from the last to the first. That way if the one I'm working with gets removed, it doesn't affect the next ones (previous ones) at all...

ando

-----------------------
Ando Sonenblick
SpriTec Software
www.spritec.com
On Jul 22, 2004, at 5:33 PM, Jack Christensen wrote:

What is the prefered way to iterate through a table while checking for some condition and selectively removing fields? I seem to recall that directly setting them to nil caused the next() function to get confused.

This is what I'm doing now. It works but it seems there should be a better way.

local removeList = {} -- list to hold values to remove since we can't remove in the loop without messing it up
   for k, v in pairs( self.eventList ) do
       if v.time <= self.time then
           v.event( self )
           removeList[ k ] = k
       end
   end
     for k, v in pairs( removeList ) do
       self.eventList[ k ] = nil      end

Jack