lua-users home
lua-l archive

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

> assert(#T1 == #T2)  -- will fail

You cannot compare tables like that. Two variables containing a table
will be equal only if they are in fact pointing to the same table, not
if the table has the same contents. (Unless you changed the behavior
with metatables)

> ----------------------------------------
> T1={}
> for i = 2, 4 do
>     T1[i] = {}
> end
> print(#T1)
> ----------------------------------------

# is not biuniquely defined for non linear tables that do not go from
1...n.  You can read up the exact definition of #, but usually just
don't do it for something that is not defined 1..n.
> I don’t know why any of this is happening, but it seems extremely buggy to me. Why are my tables that are missing a [1] key returning a size? ipairs() will not traverse them, but (for i=1, #table) will? This should not be.

Read it up in the Lua manual.

"The length operator is denoted by the unary operator #. The length of
a string is its number of bytes (that is, the usual meaning of string
length when each character is one byte).

The length of a table t is defined to be any integer index n such that
t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be
zero. For a regular array, with non-nil values from 1 to a given n,
its length is exactly that n, the index of its last value. If the
array has "holes" (that is, nil values between other non-nil values),
then #t can be any of the indices that directly precedes a nil value
(that is, it may consider any such nil value as the end of the array)"