lua-users home
lua-l archive

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


My opinion is that all proposed operators to get the length of a table are too cryptic to be used with a language with an intuitive syntax as Lua. A more intuitive length operator, at least for me, is |t|. Of course |s| could also be used to get the length of a string.

With this operator, appending a value to a table could be written as t[|t|]=value. And t[]=value could be used as a syntatic sugar for appending values, but I also like the t=t..value idea.

Just to make it clear, by length of a table I mean the index of the last non-nil element plus 1, or 0 on an empty table. This value can be stored as part of the table structure and updated as needed on the C side:

t={}
print(|t|) -- 0
t[|t|]='a' -- 'a' stored at index 0
print(|t|) -- 1
t[5]='b'
print(|t|) -- 6