lua-users home
lua-l archive

Modifying a table while iterating over it?

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


What happens when you modify a table while iterating over it?

I would expect the following iteration to proceed unaffected by the modifications, but I’m not certain...

for key, value in pairs(someTable) do

if someArbitraryCondition() then

someTable[key] = nil

end

end

I would expect the following code to behave unpredictably regarding whether or not the new items appear in the iteration.

for key, value in pairs(someTable) do

if someArbitraryCondition() then

someTable[newKey()] = newValue()

end

end

Can anyone confirm or deny my suspicions?


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