lua-users home
lua-l archive

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



On Jun 24, 2009, at 10:19 AM, John Hind wrote:

In my opinion (for what little it is worth) it would be useful if the Lua core kept a count of the total number of elements in a table. Then it could compare the "array count" with the "true count".

What does this buy you?

Assuming you mean "true count" is the number of non-nil table entries, and "array count" is the result of the # operator, if they are equal you still don't know what you have:

> t = {}
> t[1] = 1
> t[3] = 3
> t[999] = 999
> =#t
1
> t[2] = 2
> =#t
3
> t[1] = nil
> return #t
3
> i = 0
> for _,_ in pairs(t) do i = i + 1 end
> =i
3


e