lua-users home
lua-l archive

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


It was thus said that the Great Duane Leslie once stated:
> 
> When I spoke about *generic* I meant a length value that was always equal
> to the largest index in the table.

  There are problems with assuming the length of the "array" portion of a
table is the largest integer key:

	-- Assume that the length of a Lua sequence is the largest
	-- integer index in the table.

	x = { [500] = true }

	-- The length of x is now 500

	table.remove(x)

	-- This removes an entry from the array and moves everything down
	-- one spot (documented behavior).  By default, table.remove()
	-- removes the last element, so there shouldn't be an entry for
	-- x[500]

	print(x[500])

	-- well, look at that!  Now, look at this:

	table.remove(x,1)

	-- This should remove the first element and shift everything thing
	-- down one.  So x[500] will now be x[499]

	print(x[499])

	-- hmmm ..

	print(x[500])

	-- Um ...

  This doesn't quite work for stock Lua.

  -spc