How can I safely iterate a lua table while keys are being removed

前端 未结 2 1030
花落未央
花落未央 2021-01-11 23:01

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

相关标签:
2条回答
  • 2021-01-11 23:10

    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:

    1. Make an empty table for table additions.
    2. Start iterating over your main table.
    3. As you find entries you want to modify, modify them in-place. (This is permitted.)
    4. As you find entries you want to delete, delete them in-place. (This is permitted.)
    5. As you find entries you want to add, add them into the other table that started empty.
    6. When you finish your iteration, merge the addition table into the main table.
    7. Lather. Rinse. Repeat.

    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.

    0 讨论(0)
  • 2021-01-11 23:19

    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.)

    0 讨论(0)
提交回复
热议问题