Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to print _G doesn't work

Tags:

lua

According to the documentation _G "holds the global environment". I wanted to see what's inside it so I wrote the following code to print _G but it doesn't work:

function f(x)
    return 2*x
end
a=3
b="hello world"
print("_G has "..#_G.." elements")
for k,v in pairs(_G) do
    print(k)
    print(_G[k])
    print("G["..k.."]=".._G[k])
end

Error:

_G has 0 elements
a
3
G[a]=3
string
table: 003C8448
lua: try_G.lua:10: attempt to concatenate field '?' (a table value)
stack traceback:
    try_G.lua:10: in main chunk
    [C]: ?
>Exit code: 1
like image 323
AlexStack Avatar asked Nov 25 '25 09:11

AlexStack


1 Answers

You could also use the table.foreach(t,f) function. It iterates over a table t, calling the function f with each key and value pair. Use with print to get a quick view:

table.foreach(_G,print)

This is really handy at the interactive prompt as it is reasonably succinct and easy enough to type.

C:\Users\Ross>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> table.foreach(_G,print)
string  table: 005CE3D0
xpcall  function: 00717E80
package table: 005CE088
tostring        function: 00717DE0
print   function: 00711CB8
os      table: 005CE358
unpack  function: 00717E40
require function: 00718360
getfenv function: 00711B58
setmetatable    function: 00717DA0
next    function: 00711C38
assert  function: 00711A38
tonumber        function: 00717DC0
io      table: 005CE218
rawequal        function: 00711CF8
collectgarbage  function: 00711A78
getmetatable    function: 00711B98
module  function: 00718320
rawset  function: 00711D58
math    table: 005CE448
debug   table: 005CE498
pcall   function: 00711C78
table   table: 005CE128
newproxy        function: 00711E10
type    function: 00717E00
coroutine       table: 005CDFE8
_G      table: 00713EC8
select  function: 00711D98
gcinfo  function: 00711B18
pairs   function: 00711F98
rawget  function: 00711D18
loadstring      function: 00711C18
ipairs  function: 00711F68
_VERSION        Lua 5.1
dofile  function: 00711A98
setfenv function: 00717D60
load    function: 00711BD8
error   function: 00711AD8
loadfile        function: 00711BB8
>

Update: Unfortunately, as Alexander Gladysh reminds me, the table.foreach function was deprecated in Lua 5.1, and a quick check of the current beta release of 5.2 shows that it has been removed in Lua 5.2. It is easy to write the same loop in terms of pairs:

for k,v in pairs(_G) do print(k,v) end

which should give the same output as table.foreach(_G,print) would. The key feature that I'm leaning on here is that print is defined to call tostring() on each argument you pass, and tostring() is defined to return some sort of sensible string for every kind of value, even those like functions that don't have a good representation as a string. The details will differ on each platform, but the default implementation of tostring() includes the address of the table or function in its string result, allowing you to at least recognize that _G.os and _G.io are distinct tables.

For more human-friendly table printing, there are a lot of solutions, ranging from examples in PiL to several persistent data libraries. Personally, I like the pl.pretty.write() function provided by steve donavan's penlight library.

like image 85
RBerteig Avatar answered Nov 27 '25 01:11

RBerteig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!