如何从数组中删除所有“nil”值?

man*_*eak 3 arrays null lua lua-table

我有一个对象数组(或只是数字),还有另一个数组,其中包含在任何情况下都不应从第一个数组中删除的所有对象。它看起来像这样:

-- Array of objects (just numbers for now)
Objects = {}

-- Array of objects that should always stay in the 'Objects' array
DontDestroyThese = {}

-- Populate the arrays
Objects[#Objects+1] = 1
Objects[#Objects+1] = 2
Objects[#Objects+1] = 3
Objects[#Objects+1] = 4
Objects[#Objects+1] = 5

DontDestroyThese[#DontDestroyThese+1] = 2
DontDestroyThese[#DontDestroyThese+1] = 5
Run Code Online (Sandbox Code Playgroud)

现在,我有一个名为的方法,destroy()该方法应该从数组中删除除Objects数组中包含的对象之外的所有对象DontDestroyThese。该方法看起来像这样:

function destroy()
    for I = 1, #Objects do
        if(DontDestroyThese[Objects[I]] ~= nil) then
            print("Skipping " .. Objects[I])
        else
            Objects[I] = nil
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

然而,结果是,Objects数组现在nil到处包含值。我想删除这些nils ,以便Objects数组仅包含调用后留下的数字destroy()。我怎么做?

ton*_*mtr 6

我认为解决方案要简单得多。要删除任何 nils(数组中的“洞”),您所需要做的就是使用pairs() 迭代您的表。这将跳过任何 nils,仅返回您添加到“cleanup”函数末尾返回的新本地表中的非 nil 值。数组(索引从 1..n 开始的表)将保持相同的顺序。例如:

function CleanNils(t)
  local ans = {}
  for _,v in pairs(t) do
    ans[ #ans+1 ] = v
  end
  return ans
end
Run Code Online (Sandbox Code Playgroud)

然后你只需要这样做:

Objects = CleanNils(Objects)
Run Code Online (Sandbox Code Playgroud)

要测试它:

function show(t)
  for _,v in ipairs(t) do
    print(v)
  end
  print(('='):rep(20))
end

t = {'a','b','c','d','e','f'}
t[4] = nil          --create a 'hole' at 'd'
show(t)             --> a b c
t = CleanNils(t)    --remove the 'hole'
show(t)             --> a b c e f
Run Code Online (Sandbox Code Playgroud)


Ala*_*lan 5

最有效的方法可能是创建一个新表来保存结果。尝试在数组中移动值可能比简单地附加到新表具有更高的开销:

function destroy()
    local tbl = {}
    for I = 1, #Objects do
        if(DontDestroyThese[Objects[I]] ~= nil) then
            table.insert(tbl, Objects[I])
        end
    end
    Objects = tbl
end
Run Code Online (Sandbox Code Playgroud)

此方法还意味着您不必处理更改正在迭代的表/数组的内容。