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 <>
Reply-To: Lua list <>
To: Lua list <>
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-+
|    | UN-altered REPRODUCTION and DISSEMINATION of this
| ( | IMPORTANT information is ENCOURAGED
+- --+

<< attach3 >>