lua-users home
lua-l archive

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


At 06:44 PM 4/16/2012, Matthew Wild wrote:
>On 17 April 2012 02:29, chris <csrl@gmx.com> wrote:
>> Matthew Wild <mwild1 <at> gmail.com> writes:
>>> On 16 April 2012 09:31, <csrl <at> gmx.com> wrote:
>>> > My point in this is that if #t returns a particular value n,
>>> > it becomes surprising if each key of {1..n} does not index a
>>> > non nil value in the table.
>>>
>>> It might be surprising, until you read the documentation.
>>
>> Reading the documentation obviously did not clarify it for me,
>> hence my post. :(
>>
>> ....
>> To me the documentation stated that if the list was not a
>> sequence that the length of the table is not defined, meaning
>> that it is nil. As that is not what my testing showed me, instead >> it showed rather aberrant behavior which caused confusion. Slightly >> better wording would have been to say that in such a case the result
>> of the length operator is 'undefined'.
>
>Aha! Now I understand your issue. I can see how you might read the
>text that way.

At least it is defined in one sense: You can be confident that the expression (t[#t+1] == nil) is true for all tables t, regardless of whether t holds a valid sequence or not. Note that you cannot assume that (t[#t] ~= nil) holds,
because if t holds an empty sequence, #t is expected to be zero.


Ross Berteig                               Ross@CheshireEng.com
Cheshire Engineering Corp.           http://www.CheshireEng.com/