lua-users home
lua-l archive

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

On 4 March 2010 02:20, M Joonas Pihlaja <> wrote:
> The short answer is: because Lua is on crack.
> The slightly longer answer is: #t is defined to return an index i of
> such that t[i] is non-nil and t[i+1] is nil, or zero if no such index
> exists.

It’s the price to pay for having cheap (memory and performance) sparse arrays.

t = { [1000000] = 1 }

... is very cheap. Lua tables are key-value pairs at their heart. They
can behave like arrays if you treat them with care (keeping them not
sparse), but C-like arrays are only one data structure of the many
that are possible with tables.