• Subject: Re: Why is nil not a valid table key?
• From: Chris Marrin <chris@...>
• Date: Sun, 27 Nov 2005 09:52:19 -0800

```Rici Lake wrote:
```
```
On 26-Nov-05, at 10:29 AM, Mike Pall wrote:

```
```Lua signals the end of a table traversal with a nil key. This is
both simple and efficient.
```
```
```
It is pretty easy to store a 'length' property in a table to identify how many numeric keys it has. I found that technique ALMOST works in Lua. If a table has entries for numeric keys 1,2,12 and 17, and I set the length property to 17 then I can go:
```
for i=1,t.length do
print(t[i])
end

```
and I will get all the numbers printing out, with nils in the slots that don't have a numeric index.
```
But I found that attempting to set:

t[0] = "hello"

```
does not work. When I read this back, it prints nil. Is this true, or do I have a bug in my logic somewhere? I thought Lua only cared about "arrays starting at 1" when you fill a table like this:
```
local t = { 12,17,627 }

```
In which case t[1] == 12. But can you not even set a numeric property on a table with the numeric key of 0? If so, that seems very broken...
```
--
chris marrin              ,""\$, "As a general rule,don't solve puzzles
chris@marrin.com        b`    \$  that open portals to Hell" ,,.
,.`           ,b`    ,`                            , 1\$'
,|`             mP    ,`                              :\$\$'     ,mm
,b"              b"   ,`            ,mm      m\$\$    ,m         ,`P\$\$
m\$`             ,b`  .` ,mm        ,'|\$P   ,|"1\$`  ,b\$P       ,`  :\$1
b\$`             ,\$: :,`` |\$\$      ,`   \$\$` ,|` ,\$\$,,`"\$\$     .`    :\$|
b\$|            _m\$`,:`    :\$1   ,`     ,\$Pm|`    `    :\$\$,..;"'     |\$:
P\$b,      _;b\$\$b\$1"       |\$\$ ,`      ,\$\$"             ``'          \$\$
```"```'"    `"`         `""`        ""`                          ,P`

```