-- @runtimes lua54 luajit --- 演示:遍历中删除元素要小心;先收集再删。 local M = {} --- @return table { remaining = table } function M.run(log) log = log or print local t = { 1, 2, 3, 4, 5 } local to_remove = {} for index, value in ipairs(t) do if value % 2 == 0 then to_remove[#to_remove + 1] = index end end for i = #to_remove, 1, -1 do table.remove(t, to_remove[i]) end log(("remaining=%s"):format(table.concat(t, ","))) return { remaining = t } end if not ... then M.run() end return M