digit strings as table indexes

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

digit strings as table indexes

Jimmie Houchin-3
Okay, I learned that '000' is not a valid name in Lua.
But for whatever reason it is accepted successfully as a table index.
I don't if that is something I can depend on or not.

If I don't care about losing the syntactic sugar of t.000 for access,
are there other inherent problems with using digit strings to index tables?

I don't have a lot of programming experience, so I am looking for some wisdom from those who are further down this journey.

So many things are identified by numbers, products, people, books, etc.
Numbers as unique identifiers are common. And especially since nothing starting with a digit is a valid name, regardless of subsequent alphanumeric characters. That invalidates lots of ids for objects.

But I don't know if I'll run into problems continuing down that road.

I don't want to build a huge people database indexing them by their SSN (U.S. Social Security Number) and then find I am using poor practices.

Would it be better, even if seemingly uglier (to me) to use
'ssn123-45-6789' instead of simply '123-45-6789'
which would make the digit string into a legitimate Lua name.

Any wisdom, knowledge and understanding greatly appreciated.

Thanks.

Jimmie

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Rici Lake-2

On 25-Jan-07, at 11:22 AM, Jimmie Houchin wrote:

Okay, I learned that '000' is not a valid name in Lua.
But for whatever reason it is accepted successfully as a table index.
I don't if that is something I can depend on or not.

Yes, you can.

If I don't care about losing the syntactic sugar of t.000 for access,
are there other inherent problems with using digit strings to index tables?

No, there aren't. :)

Any Lua object except nil [see note] can be used as a table key. That includes strings (of any kind, which can include \0), numbers (including numbers like 0.5), tables and functions.

Names are just a feature of the compiler: once the script is compiled, there is no difference between a.john and a["john"]. They compile to the same virtual machine code.

[Note: Technically, there is one other object which cannot be used as a key: the "not a number" (NaN) value which can results from invalid mathematical operations like 0/0. That's because, according to "standard" (IEEE-754) semantics, NaN is not equal to itself.]



Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Jimmie Houchin-3
Rici Lake wrote:

On 25-Jan-07, at 11:22 AM, Jimmie Houchin wrote:

Okay, I learned that '000' is not a valid name in Lua.
But for whatever reason it is accepted successfully as a table index.
I don't if that is something I can depend on or not.

Yes, you can.

If I don't care about losing the syntactic sugar of t.000 for access,
are there other inherent problems with using digit strings to index tables?

No, there aren't. :)

Any Lua object except nil [see note] can be used as a table key. That includes strings (of any kind, which can include \0), numbers (including numbers like 0.5), tables and functions.

Names are just a feature of the compiler: once the script is compiled, there is no difference between a.john and a["john"]. They compile to the same virtual machine code.

[Note: Technically, there is one other object which cannot be used as a key: the "not a number" (NaN) value which can results from invalid mathematical operations like 0/0. That's because, according to "standard" (IEEE-754) semantics, NaN is not equal to itself.]

Fantastic!

Thanks for the reply.

I just didn't want to get down the road aways and then get bit by erroneous assumptions on my part.

These IDs just seemed like such natural indexes that I didn't want to have to code around them if I didn't have to.

Thanks again. :)

Jimmie

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

David Given
Jimmie Houchin wrote:
[...]
> These IDs just seemed like such natural indexes that I didn't want to 
> have to code around them if I didn't have to.

Do remember to distinguish between a key that's a number (as in table[4]) and
a string containing digits (as in table["4"]). Both are acceptable, but
they're different keys.

The . syntax for tables is semantic sugar for using a string key, so
table.four and table["four"] are equivalent --- but in this case, the thing
after the . must be a valid keyword, and there are strict rules about that.
When in doubt, use [] syntax.

(Typically, . syntax is only really used for objects and structures. If you're
using a table as an actual dynamic storage device, it's recommended to use [].)

-- 
âââ ïïïïïïïïïïïïïï âââ http://www.cowlark.com âââââââââââââââââââ
â
â "There does not now, nor will there ever, exist a programming language in
â which it is the least bit hard to write bad programs." --- Flon's Axiom

Attachment: signature.asc
Description: OpenPGP digital signature

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Sam Roberts-2
In reply to this post by Jimmie Houchin-3
On Thu, Jan 25, 2007 at 10:22:09AM -0600, Jimmie Houchin wrote:
> Would it be better, even if seemingly uglier (to me) to use
> 'ssn123-45-6789' instead of simply '123-45-6789'
> which would make the digit string into a legitimate Lua name.

Actually, it wouldn't, ssn123-45-6789 is not a lua "name":

% lua
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
> ssn123-45-6789 = 3
stdin:1: `=' expected near `-'
> ssn123 = 3
> print(ssn123-45-6789)
-6831



Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Javier Guerra Giraldez
In reply to this post by Jimmie Houchin-3
On Thursday 25 January 2007 11:22 am, Jimmie Houchin wrote:
> Okay, I learned that '000' is not a valid name in Lua.
> But for whatever reason it is accepted successfully as a table index.
> I don't if that is something I can depend on or not.

you're mixing two different concepts used at very different parts of the 
language: names and values.

a value can be a lot of things: a number, a string, nil, a table, a function, 
true, false... (anything else?)

you can use as table keys any value except nil and NaN, as Rici explained.

a Name, OTOH, is a parser concept.  its a small part of your source code, you 
can use names to identify variables or special words.  since the parser can 
pick a name from the source code, and any name can be expressed as a string 
value, there's a nice notation to use tables as 'records', or 'objects'.

simply put:

table.name  ==== table["name"]

this is called "syntactic sugar", because it makes code more attractive, but 
don't add any nutritional value.  the compiled code is exactly the same on 
both cases.

but, since there's no quoting around the name in the left form, you can't 
expect it to work with keys that are somewhat 'ugly'

some examples:

table["two words"]			-- ok
table.two words				-- wrong!!

table["123"]				-- ok
table.123					-- wrong!!

table["this-that"]			-- ok
table.this-that				-- wrong!!

table["you!@#$@#$#%"]	-- ok
table.you!@#$@#$#%		-- wrong!!

table["_"]					-- ok
table._					-- wrong!

also, since the dot notation is sugar for _string_ values, you have to watch 
out when the key isn't a string:

table[true]				-- key is the boolean true value 
table.true					-- key is the string "true"

table[123]					-- key is a number
table["123"]				-- key is a string

table[var]					-- key is the value of the variable
table.var					-- key is the string "var"


in short: you are really free to use almost anything as a table key, the 
limitation is only for the "dot notation" syntactic sugar.

as David noted, you should only use dot notation for constant keys, typically 
record fields, object members and such.  for anything else (especially when 
the key is variable, and user entered!) use the 'real' form: []

-- 
Javier

Attachment: pgphFRCwzrjbb.pgp
Description: PGP signature

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Jimmie Houchin-3
In reply to this post by David Given
David Given wrote:
Jimmie Houchin wrote:
[...]
These IDs just seemed like such natural indexes that I didn't want to have to code around them if I didn't have to.

Do remember to distinguish between a key that's a number (as in table[4]) and
a string containing digits (as in table["4"]). Both are acceptable, but
they're different keys.

Yes, I understand. It generally won't be much of an issue since most of these are not simple numbers, but aren't valid Names either. But thanks for the heads up.

The . syntax for tables is semantic sugar for using a string key, so
table.four and table["four"] are equivalent --- but in this case, the thing
after the . must be a valid keyword, and there are strict rules about that.
When in doubt, use [] syntax.

Yes. Some of this is simply exploring and learning as I go.

(Typically, . syntax is only really used for objects and structures. If you're
using a table as an actual dynamic storage device, it's recommended to use [].)

Absolutely. My program will use things dynamically and thus use [].

But while playing, testing, experimenting in the interpreter I try the . syntax for learning.

Thanks for the reply and the education. It helps.

Jimmie


Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Jimmie Houchin-3
In reply to this post by Sam Roberts-2
Sam Roberts wrote:
On Thu, Jan 25, 2007 at 10:22:09AM -0600, Jimmie Houchin wrote:
Would it be better, even if seemingly uglier (to me) to use
'ssn123-45-6789' instead of simply '123-45-6789'
which would make the digit string into a legitimate Lua name.

Actually, it wouldn't, ssn123-45-6789 is not a lua "name":

% lua
Lua 5.0.2  Copyright (C) 1994-2004 Tecgraf, PUC-Rio
ssn123-45-6789 = 3
stdin:1: `=' expected near `-'
ssn123 = 3
print(ssn123-45-6789)
-6831

Wow, I just got bit by another assumption on my part. Oops. :)
Obviously I wasn't thinking. Nor have I attempted such. I would definitely have to remove the dashes to make such valid.

Thanks for pointing that out to me.

It helps in training my mind to spot thinking errors.

Jimmie

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Romulo Bahiense
In reply to this post by Javier Guerra Giraldez
Javier Guerra wrote:
On Thursday 25 January 2007 11:22 am, Jimmie Houchin wrote:

(...)

table["_"]					-- ok
table._					-- wrong!

also, since the dot notation is sugar for _string_ values, you have to watch out when the key isn't a string:

table[true] -- key is the boolean true value table.true -- key is the string "true"

(...)

< table._ > is valid, but < table.true > isn't ( 'true' is a keyword )

--rb


Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Javier Guerra Giraldez
On Thursday 25 January 2007 1:51 pm, Romulo Bahiense wrote:
> < table._ > is valid, but < table.true > isn't ( 'true' is a keyword )

right!  should've tested before pressing send...

OT: i have a spellchecker always active in my emailer (kmail); wouldn't it be 
nice to have a Lua interpreter embedded to test code while writing? :-)


-- 
Javier

Attachment: pgpUcdNV2IpVD.pgp
Description: PGP signature

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Aaron Brown
Javier wrote:

OT: i have a spellchecker always active in my emailer
(kmail); wouldn't it be nice to have a Lua interpreter
embedded to test code while writing? :-)

Yet another advantage of composing mails in an external
editor!

--
Aaron

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Jimmie Houchin-3
In reply to this post by Javier Guerra Giraldez
Javier Guerra wrote:
On Thursday 25 January 2007 11:22 am, Jimmie Houchin wrote:
Okay, I learned that '000' is not a valid name in Lua.
But for whatever reason it is accepted successfully as a table index.
I don't if that is something I can depend on or not.

[snip a mighty fantastic reply!]


Wow!!!

Thanks for the education.

What a great list!

Jimmie

Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

Philippe Lhoste
In reply to this post by David Given
David Given wrote:
The . syntax for tables is semantic sugar for using a string key, so
table.four and table["four"] are equivalent --- but in this case, the thing
after the . must be a valid keyword, and there are strict rules about that.

For the record, you had a poor choice of word here... ;-)
"The thing after the . must be a valid identifier which must not be a keyword" would have been better...

foo.doh is valid;
foo.for isn't.

And although legal, using 'table' as a table name, although tempting, even more for small demonstration snippet, is not a good idea, as you probably will mask the 'table' table from the standard library.

I just write that so some newbies reading this thread won't fall in some pitfalls! :-D

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --


Reply | Threaded
Open this post in threaded view
|

Re: digit strings as table indexes

David Given
Philippe Lhoste wrote:
[...]
> And although legal, using 'table' as a table name, although tempting, 
> even more for small demonstration snippet, is not a good idea, as you 
> probably will mask the 'table' table from the standard library.

Ack! You're right. It was late. The light was bad. My condition was acting up.
She *said* she was over 18. The fuel gauge said the tank was full Of course I
have a backup, somewhere. But I thought that was *your* job. It must be due to
cosmic rays caused by reentering satellite debris...

-- 
âââ ïïïïïïïïïïïïïï âââ http://www.cowlark.com âââââââââââââââââââ
â
â "There does not now, nor will there ever, exist a programming language in
â which it is the least bit hard to write bad programs." --- Flon's Axiom

Attachment: signature.asc
Description: OpenPGP digital signature