lua-users home
lua-l archive

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



On 27 Jun 2016, at 09:23, GHui <ugiwgh@qq.com> wrote:

I very confuse of tabe, only the 3rd can be skiped. 
The test as following:
> d={}
> d[1]=1
> d[2]=2
> d[3]=3
> d[5]=5
> print(#d)
3

> a={}
> a[1]=1
> a[3]=3
> print(#a)
1

> b={}
> b[1]=1
> b[2]=2
> b[4]=4
> print(#b)
4


In Lua there is no native array type. To use them you use a table, but it’s up to you to make sure that all entries have a value. So in your examples you created tables with ‘holes’ in them, and then the length operator # has an “undefined” result (as you noticed).

see https://www.lua.org/manual/5.1/manual.html#2.5.5

hth
Thijs