lua-users home
lua-l archive

Re: Removing while iterating through table

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


thx for the point out... you are right... I notice that PIL always uses the term "array" when a table is ordered and a "table" when not. Good way to specify, but I do wish the table weren't overridden for both types... the clarity of having a table type and an array type would be nice imho...
would definitely have prevented my quick-judgment on this matter..
ando
-----------------------
Ando Sonenblick
SpriTec Software
www.spritec.com
On Jul 22, 2004, at 8:32 PM, Virgil Smith wrote:
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

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