lua-users home
lua-l archive

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


> Lua 5.1 (on windows, if it matters)
>
> z = {}
> z.z = {}
>
> works
>
> local z={}
> z.z = {}
>
> does not work
>
> stdin:1: attempt to index global 'z' (a nil value)
> stack traceback:
> stdin:1: in main chunk
> [C]: ?
>
> What is happening?

You are in the Lua interpreter, every line is its own chunk
(i.e. local variables are limited to the line they are defined on).

You can work around it with a do/end block:

    Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
    > local z={}
    > z.z = {}
    stdin:1: attempt to index global 'z' (a nil value)
    stack traceback:
        stdin:1: in main chunk
        [C]: in ?
    > do
    >> local z={}
    >> z.z = {}
    >> end
    >

--
Pierre Chapuis