[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Removing while iterating through table
- From: "Virgil Smith" <Virgil@...>
- Date: Thu, 22 Jul 2004 22:32:01 -0500
Ando, note that the original poster used "pairs" and not "ipairs".
Working backward is simple if you are counting down through the "array"
section of a table, but there is no way to "iterate backward" through a Lua
table in general (AFAIK).
----------
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
>