lua-users home
lua-l archive

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


RLake@oxfam.org.pe wrote:
table.getn() does not return the size of the table; it
only concerns itself with entries whose keys are positive
integers. (In fact, it is simply, in effect, an attribute
of the table, and can be set to an arbitrary value with
table.setn(), but if that hasn't been done table.getn()
searches for the largest integer key.)

To be accurate (to avoid confusion for newbies...), if the n field of the table
hasn't been changed, table.getn() returns the largest integer key when counting
consecutive indexes from 1.
I feel this sentence can be better formulated...

In other words, the array part of a table only have consecutive positive integer keys
starting at 1. Any other positive integer key, if there is a gap, isn't seen as index
of the array.

Exemple:
array = { [9]=1, [7]=5, [5]=12.3, [3]=0.25, [2]=-45.01, [1]=7 }
print(table.getn(array))
3

Likewise, ipairs will interate only on 1, 2, and 3.
and a = { [2] = 1, [3] = 4 } isn't an array (not starting at 1).

--
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/ (outdated)
http://philho.multimania.com (in French, for files to download)
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--