lua-users home
lua-l archive

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




On 26/09/15 03:58 PM, Dirk Laurie wrote:
2015-09-26 17:44 GMT+02:00 Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>:
Try this:

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]~=ni then
                 n=n+1
                 list[n]=list[i]
         end
end
list.n=n

for k,v in ipairs(list) do print(k,v) end
This is a little like one of my solutions, but neater. Thanks.

However, #list is still 10. One needs, before resetting list.n,

for i=n+1,list.n do list[i]=nil end

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

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.