lua-users home
lua-l archive

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


On 12/28/2010 09:25 AM, Dirk Laurie wrote:
How does one iterate over all the keys of a table _except_
the ones that ipairs would reach?

Unsuccessful attempts so far:

for k,v in pairs(t) do if not tonumber(k) then ...

for k,v in pairs(t) do if type(k)!='number' then ...

for k,v in pairs(t) do if type(k)!=number or k>#t then ...

for k,v in hpairs(t) do ...

Dirk

Is there something inherently Bad about doing this?

for k,v in next, t, #t > 0 and #t or nil do
   ...
end

I use this for a JSON serializer to encode the hash part, and it seems to work, but that may be implementation specific and not reliable (i.e. it relies on next() returning the first key in the hash part when given the last key in the array part). Critique?

Cheers,
Richard