[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: length operator [was: table insertion]
- From: leiradella@...
- Date: Fri, 05 Nov 2004 07:19:23 -0500 (EST)
- Date-warning: Date header was inserted by mgate.usa.xerox.com
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:
print(|t|) -- 0
t[|t|]='a' -- 'a' stored at index 0
print(|t|) -- 1
print(|t|) -- 6