lua-users home
lua-l archive

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

Stefan Brantschen wrote:
>   t = {}
>   table.insert(t, "a")
>   t[1] = nil
>   print (table.getn(t))


table.insert and table.getn only work on arrays. An array is, AIUI, a
table with a linear sequence of non-nil elements with numeric indices
starting at 1.

As soon as you start setting numeric elements to nil, or even if you
start setting them beyond the end of the array, then it's not a valid
array any more and all these things start producing undefined behaviour.

To remove an element from an array, I believe you have to use

(Arrays can coexist with non-numeric indices. This includes indices with
numeric values below 1, so t[0], t[-1] etc do not affect the array-ness
of the table.)

+- David Given --McQ-+ "You cannot truly appreciate _Atlas Shrugged_
|    | until you have read it in the original Klingon."
| ( | --- Sea Wasp on r.a.sf.w
+- --+

Attachment: signature.asc
Description: OpenPGP digital signature