lua-users home
lua-l archive

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


next(t, #t) returns the first key in the hash part (if any).
This isn't necessarily true. It is possible to set up a table with
multiple holes in the array part such that next( t, #t ) will return
an index which still falls in the array.

I wasn't careful in my phrasing of that sentence. Yes, it may return a
key that follows one of the holes, but this is exactly what I'd
need/expect in this case.

For example, using this table, {"a", nil, nil, "f", "g", [7] = {}, [9]
= "i"}, #t gives me 5 (Lua 5.1) and next(t, #t) returns 7.

Just keep in mind this behaviour is pretty much specific to table constructors in current implementation, and is (I believe) completely undefined.

If you build the same table differently:

t = {}
t[1]='a'
t[2]=nil
t[3]=nil
t[4]='f'
t[5]='g'
t[7]={}
t[9]="i"

=#t
1
=next(t,#t)
5       g