# 03 · delete-while-iterating **演示**:先收集要删的下标,再倒序删除,避免正序删除跳过元素。 **运行时**:`lua54`、`luajit`。 **运行**: ```sh .tools/bin/lua54 scripts/run-example.lua examples/12-iterators/03-delete-while-iterating .tools/bin/lua54 scripts/test-example.lua examples/12-iterators/03-delete-while-iterating ``` **预期输出**(lua 5.4.9 / macOS arm64): ```text remaining=1,3,5 ``` **看点**:`table.remove` 会移动元素,正序删除会跳过下一项。 **注意**:PUC-Lua 允许遍历时把当前值设为 nil,但跨实现不保证。