In my main coroutine, I am removing or adding entries from a table depending on user operations. In the background, I'd like to iterate over the entries in the table. I don't mind particularly if I miss an insertion on one iteration, providing I can catch it before the next.
Is it safe to iterate over it with pairs
? Or should I use next
instead?
You can safely remove entries while traversing a table but you cannot create new entries, that is, new keys. You can modify the values of existing entries, though. (Removing an entry being a special case of that rule.)
You can't get there from here. At least not directly....
As lhf said you can modify or remove entries while traversing a table but you cannot add them. The results are ... undefined. (Read: branch into hyperspace or equivalent for all practical purposes.)
If you insist on being able to add entries you're going to have to clone your table and use one copy for iteration and the other for keeping track of your insertions and deletions. If this itself doesn't match your requirements, you'll have to instead do something like this:
There are other similar patterns with slightly different rules you can employ. For example between steps 5 & 6 you might want to insert a recursive call to your table walking code for the added table entries before merging, etc. You may also have to keep track of possible deletions in both the main table and in your additions table if that is a possible interaction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With