lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Sep 27, 2015 at 4:25 PM, Soni L. <fakedme@gmail.com> wrote:
Would this work?

list=table.pack(1,2,3,nil,4,5,nil,nil,6,7)

local n=0
for i=1,list.n do
        if list[i]~=nil then
                n=n+1
                list[n]=list[i]
                list[i]=nil
        end
end
list.n=n

for k,v in ipairs(list) do print(k,v) end

Nice idea!
But it requires a little bit more accurate implementation:

list=table.pack(1,2,3,nil,4,5,nil,nil,6,7)

local n, tmp = 0
for i=1, list.n do
   if list[i] ~= nil then
      tmp, n, list[i] = list[i], n + 1
      list[n] = tmp
   end
end
list.n = n

for k,v in ipairs(list) do print(k,v) end