lua-users home
lua-l archive

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


On Tue, Jan 23, 2007 at 05:57:18PM -0600, Jimmie Houchin wrote:
> I have had this happen a couple of times to me.
> I don't know if this how it is supposed to be and I am misunderstanding something.
> 
> > =items
> table: 0x807e760
> > printt(items)
> 0007126409      table: 0x8089bf0
> 0007164653      table: 0x80abf38
> 000215949X      table: 0x80ea8f0
> 0007157207      table: 0x80f52f0
> 000100039X      table: 0x80d3458
> 0007145160      table: 0x80d5348
> 0007744684      table: 0x80da320
> 0007133103      table: 0x80d9290
> 0007104065      table: 0x8092438
> 0007161069      table: 0x8096cc8
> > =#items
> 0
> >
> 
> I got tired of doing
> for k,v in pairs(t) do print(k,v) end
> so I assigned it to printt(t). :)
> 
> So I don't understand why I am getting a size of 0 for a table of 10 items?

The size of a table is not the number of key/value pair in it. It is a number n with
table[n + 1] = nil. So it only count the number of integer key and return a usefull
value only if you don't have hole in the vector part of the table.
In your case you seem to have only string key so #items return 0 because
items[0 + 1] = nil

> 
> Also I have lots of tables indexed by strings of digits as above.
> But if I try to access them via
> 
> > =items.0007126409
> stdin:1: '<eof>' expected near '.0007126409'
> 
> doesn't work, but this does.
> 
> > return items['0007126409']
> table: 0x8089bf0

The dot sugar doesn't work in this case, when lua try to read the key after the dot
he search for an identifier and found a number so the parser raise an error, so
you need to change your key for another one who start with a non-digit character or
you must use the classical notation with []

> Any help understanding what I'm currently not understanding would be greatly appreciated.
> 
> Thanks.
> 
> Jimmie

-- 
Thomas Lavergne                       "Le vrai rêveur est celui qui rêve
                                       de l'impossible."  (Elsa Triolet)
thomas.lavergne@reveurs.org                           http://reveurs.org