lua-users home
lua-l archive

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


More accurately (although slightly beside the point), table.getn consider only items numbered from 1 and up, i.e. if the table contains elements at negatiive indices or 0, any lua array functionality (like table.getn, ipairs(), etc) will skip those too. Which is to be expected, but it so happens that I bumped into this the hard way recently :P


From: David Given <dg@cowlark.com>
Reply-To: Lua list <lua@bazar2.conectiva.com.br>
To: Lua list <lua@bazar2.conectiva.com.br>
Subject: Re: newbie: problems in reading table from lua
Date: Tue, 20 Dec 2005 19:16:06 +0000

On Tuesday 20 December 2005 19:20, Alexander Miro wrote:
[...]
>     // the next function will return 0 (zero).
>     // It should return 2 (two).... WHY NOT???

Because getn only works on tables used as arrays, not tables used as
associative arrays, I'm afraid.

A table may contain some *numbered* items, and some *names* items.
table.getn() (and the low-level luaL_getn()) both consider only the numbered items. In order to extract the names items you'll need to iterate through the
table using lua_next().

--
+- David Given --McQ-+
|  dg@cowlark.com    | UN-altered REPRODUCTION and DISSEMINATION of this
| (dg@tao-group.com) | IMPORTANT information is ENCOURAGED
+- www.cowlark.com --+


<< attach3 >>