How does ipairs handle deletes from a table?
I want to have something like the following in my script:
Code:
-- remove items from currencyList that are no longer in the wallet (Sorry if this is an elementary question, but I'm still learning the LUA scripting language, and the online tutorial on it that I found didn't cover this question.) |
An interesting question, and now I know the answer myself. I suggest that whenever you have questions about how LUA operates, set up an experiment and you can discover things. For example, for your problem I wrote the following in a dummy plugin:
Code:
self.testTable = { "apple", "oranges", "cherries", "bananas", "tomato", "strawberries", "blueberries", "pears", "blackberries" } Code:
### Chat Log: General 05/21 06:06 AM ### Also, I tried to remove both "tomato" and "blueberries", which worked exactly as expected. So basically, removing a single item from a table while iterating with ipairs means you cannot access the item immediately following the item you delete. I think your best bet is to do your reverse iteration: Code:
for i = table.getn(tablename), 1, -1 do |
All times are GMT -5. The time now is 11:07 AM. |
vBulletin® - Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© MMOUI