lua-users home
lua-l archive

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


That is because of the way Lua handles tables.

It only counts to the first nil, and stops.

Since you have 'holes' in your array in the first case (It bumps into the nil), it counts 12.

But with the second case you appended right to the end...it can get the proper count.

If you need to keep a count in this case, you will need to do the tracking yourself with a counter.

Thanks,

Joel Sundquist


On Sat, Sep 14, 2013 at 10:09 PM, Jayanth Acharya <jayachar88@gmail.com> wrote:
Was mixing the table (as associated arrays) and regular array semantics, and results surprised me a bit.

[code]
-- initially declared xyz as an array of length 12
xyz = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'}

xyz[110] = 'N'
print(#xyz)

xyz[13] = 'M'
print(#xyz)
[/code]

[output]
12
13
[/output]