【发布时间】:2014-04-14 15:11:50
【问题描述】:
我正在尝试从 Lua 中的表中减去表,因此返回表将是 t1 与 t2 的减法。
这似乎有效,但有没有更有效的方法?
function array_sub(t1, t2)
-- Substract Arrays from Array
-- Usage: nretable = array_sub(T1, T2) -- removes T1 from T2
table.sort( t1 )
for i = 1, #t2 do
if (t2[i] ~= nil) then
for j = 1, #t1 do
if (t2[i] == t1 [j]) then
table.remove (t2, i)
end
end
end
end
return t2
end
local remove ={1,2,3}
local full = {}; for i = 1, 10 do full[i] = i end
local test ={}
local test = array_sub(remove, full)
for i = 1, #test do
print (test[i])
end
【问题讨论】: