Lua中插入和删除表元素

我不理解下面的代码为什么会产生错误。

代码以 main() 函数在底部开始。

heads = {}
function push(t)
    if (#t == 2) then
        table.insert(heads, t)
    end
end
function remove(id)
    for i = 1, #heads do
        if (heads[i][2] == id) then
            table.remove(heads, i)
        end
    end
end
function main()
    push({50, 1})
    push({50, 2})
    push({50, 3})
    remove(2)
end

当我运行代码时,我得到 attempt to index a nil value (field '?') 的错误。

我希望将子表元素推入表中,然后只删除第二个元素。因此,结果元素可以是 {50, 1}{50, 3}

为什么我的代码不起作用,如何解决?

点赞
用户4070330
用户4070330

根据 5.1 手册中的 table.remove 的定义:“在移除表格中的某个位置对应的元素时,如果必要的话,会将其他元素下移来填补空缺”。

在循环执行前,计算头大小(#heads)。当 i==2 时调用 table.remove,因此表格的大小缩小到 2,在下一次迭代中,您尝试索引 heads[3][2],但 heads[3] 为 nil,因此显示“尝试为 nil 值进行索引”的错误消息。

2018-07-10 14:49:18
用户2616735
用户2616735

正如Andrew所提到的,for i = 1,#heads do将去到列表的 原始 长度;如果您在循环过程中缩短了heads,那么最后的迭代将会读取heads[i]并且只会找到nil

修复这个问题的一个简单方法是通过列表 倒序 遍历,因为移除元素只影响_在_您从中删除的索引之后的索引:

for i = #heads,1-1 do
    if heads [i] [2] == id then
        table.remove(heads, i)
    end
end

注意,在任何情况下,这都是 O(n * d) 的复杂度,如果您从列表中删除许多元素,它可能非常慢。正如其他人指出的,有一种使用从v[1] = > v的映射的 O(1) 方法。

2018-07-10 16:45:02
用户1540350
用户1540350

Andrew 做得对。在遍历表时,永远不要尝试删除表中的值。这是许多语言中的常见问题。通常,您会首先存储值,然后像这样删除:

local e
for i = 1, #heads do
    if (heads[i][2] == id) then
        e = i
    end
end
if e then table.remove(heads, e) end

然而,这个解决方案很慢。只需将 ID 用作表的键即可:

local heads = {}

heads[1] = 50 -- push
heads[2] = 50
heads[3] = 50
heads[2] = nil -- remove

不需要不必要的函数调用和迭代。

2018-07-10 16:46:01
用户9383219
用户9383219

为了避免在迭代数组时删除字段引起的问题,我使用了一个带有索引变量的 while 循环,该变量在每次迭代结束时递增,但在删除索引时递减。例如,要移除所有偶数索引的元素:

local t = { 1, 2, 3, 4, 5 }
local i = 1
while t[i] do
  if t[i] % 2 == 0 then
    table.remove(t, i)
    i = i - 1
  end
  i = i + 1
end

这种方法允许你以升序迭代数组索引。

2018-07-10 19:26:20
用户20725856
用户20725856

针对这次迭代,如果有许多需要移除的元素,我宁愿选择以下代码块:

local result = {}
for _, v in ipairs(myTab) do
  if v == 'nice' then
    table.insert(result, v)
  end
end
myTab = result

因为table.remove在移除许多元素时速度较慢。

2022-12-08 16:46:40