|
filterinplace = function (cond, T) local oldsize, newsize = #T, 0 for i=1,oldsize do if cond(T[i]) then newsize = newsize + 1 T[newsize] = T[i] -- ^ or: if newsize ~= i then T[newsize] = T[i] end end end for i=newsize+1,oldsize do T[i] = nil end return T end
Sorry, but the code above does not work: t = { "a", "b", "a", } filterinplace (function (r) return r=="a" end, t) for i = 1, #t do print(i,v) end The result was: 1 nil 2 nil Regards, Tomás