lua-users home
lua-l archive

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


Because getn or # only gives you the number of array-like entries in the table!

So the length of {1,2,beta=3} is 2; {alice=1,bob=2} has length 0.

Generally to count all elements of a table, you need to do a foreach
or pairs loop.

steve d.

On Tue, Mar 25, 2008 at 9:01 AM, Raul Gerardo Huertas Paiva
<rax20037@gmail.com> wrote:
> Hello!
>
> Just yesterday I started with Lua with the intention to emebed it in my
> fututre 3D Engine. Well I have a doub aout the length operator and about the
> lengths of lists. With this code:
>
> [code]
>
> print( "Experimentando con tablas:" );
>  mitabla = { primerE = 121, segundoE = "TablaE2", tercerE = 7878.1212};
> print( mitabla.primerE );
> print( mitabla.segundoE );
> print( mitabla.tercerE );
> --Insertamos un cuarto elemento a la tabla
> print "N1 de la tabla"
>  print ( table.getn( mitabla) )
> table.insert( mitabla,  table.getn(mitabla)+1, 7854546 );
> print "N2 de la tabla"
> print ( table.getn( mitabla) )
> print( "Se va a a imprimir los valores de la tabala de manera iterativa" );
>  table.foreach( mitabla, print )
> print "Fin del contenido de la tabla"
>
>
> print( #mitabla );
> print( table.getn(mitabla) );
>
>
>
> [/code]
>
> I have this output:
>
> [code]
>
> Experimentando con tablas:
>  121
> TablaE2
> 7878.1212
> N1 de la tabla
> 0
> N2 de la tabla
> 1
> Se va a a imprimir los valores de la tabala de manera iterativa
> 1    7854546
> segundoE    TablaE2
> tercerE    7878.1212
> primerE    121
>  Fin del contenido de la tabla
> 1
> 1
>
>
> [/code]
>
> Why at the beginning, when I intialize the table it have length 0 :S ??? Why
> when I insert a value it have length 1. And why I have a expected result
> with the 'foreach' sentence.
>
> I'm using  Lua 5.1.3 last release
> Bye!
>
>
>