lua-users home
lua-l archive

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


One way to do this in Lua:

    function tsize(t)
        local c = 0
        for k,v in t do c=c+1 end
        return c
    end

There doesn't seem to be anything built into the C API for doing this
directly. I poked around a bit in ltable.c and ltable.h and don't see any
obvious way to get the information. I'd look a bit more, but I have a
class to teach now.

  - Tom Wrensch



On Wed, 27 Feb 2002, Nick Trout wrote:

> 
> >>I've got a simple question.
> I'm storing into a table several items(other tables) using a numerical index
> as key
> and I would like to know how many elements are into the table.So I tryed
> with lua_getn();
> The problem is that I'm not inserting sorted elements, for instance if I
> insert the first element with
> key 8 getn() will return 8.
> Is there any way to have the real number of "non nil" elements?
> 
> Write your own! :o)
> 
> getn returns the highest numerical key. Have a look in the manual.
> 
> nick
>