lua-users home
lua-l archive

Removing while iterating through table

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


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

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