lua-users home
lua-l archive

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




Am 14.09.2016 um 08:32 schrieb Tomas.Lavicka@tieto.com:

-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Oliver Kroth
Sent: Tuesday, September 13, 2016 12:06
To: lua-l@lists.lua.org
Subject: Re: Bug report in length of simple table



Am 13.09.2016 um 11:37 schrieb Malma:
I think this explanation must be in official doc if it's a source of
confusion.

print(# {[1] = 10, [2] = 20, [3] = nil, [4] = nil, [5] = 40} ) -- is
not sequence ?
print(# {10, 20, nil, nil, 40} ) -- is sequence ?  From doc: Note that
a table like {10, 20, nil, 40}  is not a sequence...

output:

2
5

The # operator computes the number of elements of a sequence in a Lua table # returns a valid result only for sequences.

A Lua table contains a sequence if and only if all integer keys greater zero are contiguous; i.e. there must be no missing numbers.

If there are gaps, the table contains no sequence.

Th table may contains values for other keys, including 0, negative, or non-integer numbers, this does not affect the sequence.

Note that this definition is completely independent of the implementation detail has part / array part; one does not need to care about that.
It's only about integer keys.

nil can't be stored in a table. Assigning nil to a table key removes the key. Use another value to tag special (e.g.missing) entries, like false

{10, 20, nil, nil, 40} contains NOT a sequence {10, 20, false false, 40] contains a sequence { [0]="null", 10, 20, 30, a="alpha",  [math.pi] = "pi" } contains a sequence

--
Oliver

-----------------------------------------------------------------------------------

I am new to lua and I want to understand the language. According book (Programming in Lua, 3rd edition):
"More precisely, a sequence is a table where the numeric keys comprise a set 1, . . . , n for some n." (page 24)
So table {10, nil, 30, 40} contains sequence {10} (n=1), which length is 1. Is there any other definition?
It is very confusing and operator # can be unusable for tables, because in many cases I need to check sequences and I cannot trust this operator. It should return or 1 (book definition) or if holes in sequence break the sequence, it should return 0. Otherwise I will be pushed for every sequence check to check all array fields manually in some kind of loop.

Tomas Lavicka
(please excuse my bad english - I am not english native - but I hope that you understand, what I mean)
The table {10, nil, 30, 40} does not contain a sequence in the meaning of Lua's sequence definition. For Lua a sequence *all* (integer) numbers up to the highest number 'n' must be present as key. The keys 1, 3, and 4 are present, but there is no key-value pair for the key 2.

nil is not a value that can be stored in a table. Assigning nil to a table entry deletes the entry from the table. This is different to, e.g. JavaScript, where one has to explicitly remove the key from an object.

--
Oliver