lua-users home
lua-l archive

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


Javier Guerra wrote:
On Tuesday 23 January 2007 6:57 pm, Jimmie Houchin wrote:
So I don't understand why I am getting a size of 0 for a table of 10 items?

the '#' operator works only for 'arrays' that is, tables indexed by numbers from 1 to n, without holes.

there's not a general 'table size' operator in Lua

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

from the manual:

"The syntax var.Name is just syntactic sugar for var["Name"]"
note that 'Name' isn't any kind of string; also from the manual:
"Names (also called identifiers) in Lua can be any string of letters, digits, and underscores, not beginning with a digit. This coincides with the definition of names in most languages."

so, your table's keys are not 'Names', since they begin with a digit

Thanks for the reply and the lesson.

You are so very right. I wasn't thinking clearly. I wouldn't do it in the interpreter.

I was writing a program to parse some data which was indexed by strings of digits and it just seemed natural to index the table with those identifiers. It never occurred to me that a string of digits wasn't a valid identifier or name.

I am kind of surprised I didn't get an error on the assignment of such indexes. I've got lots of them.

Thanks again for the lesson. I've got some errors to fix. :)

Jimmie