lua-users home
lua-l archive

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

On 7 May 2012 08:44, Emeka <> wrote:
> A global variable (not a function) that holds the global environment
> (see §2.2). Lua itself does not use this variable; changing its value does
> not affect any environment, nor vice-versa.
> The above is still confusing, what is it used for? And why is it there at
> all?

It is a reference to the table which contains all the global variable
(the variables you assigned without using "local"). All Lua base
functions (like print, pairs, next, tostring, tonumber, ...) are
stored in the global table. Try this in Lua interpreter:

a = 'a global variable'
print(_G['a'], _G.a) -- equivalent

To list all the global variables, you can use this as was written earlier:

for k , v in pairs(_G) do
  print(tostring(k).."  "..tostring(v))