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))

Weeelll...

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
table.remove().

(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_
|  dg@cowlark.com    | until you have read it in the original Klingon."
| (dg@tao-group.com) | --- Sea Wasp on r.a.sf.w
+- www.cowlark.com --+

Attachment: signature.asc
Description: OpenPGP digital signature