Recreation of lua's get all global functions

So recently i found the get all globals function in the lua website and i saw how horrible the code was, bad intents, and lengthy than it should be. So i recreated the lua’s function in half lines, better intents, here is the function

local function printTableAsTree(tbl, intents)
   for i, v in pairs(tbl) do
      local condition = false
      if v == _G then condition = true end -- will create an infinite loop if this line is gone
      if type(v) == "table" and not condition then
         print(string.format("%s%s %s", intents, i, type(v)))
         printTableAsTree(v, intents.."  ")
      else
         print(string.format("%s%s %s", intents, i, type(v)))
      end
   end
end
printTableAsTree(_G, "")


You may ask, whats the purpose of this? Well mindlessly surfing in the documentation or whatever it is is not ideal to get all global functions and tables, etc when this short code can do it automatically, hope this helps in any way

Globals it printed

*assert function
*getmetatable function
*rawget function
*tostring function
*rawequal function
*tonumber function
*select function
*string table
**len function
**upper function
**gsub function
**find function
**pack function
**lower function
**unpack function
**reverse function
**rep function
**packsize function
**sub function
**format function
**match function
**byte function
**gmatch function
**dump function
**char function
*type function
*io table
**write function
*os table
**date function
**clock function
**exit function
**time function
**setlocale function
**difftime function
*print function
*pcall function
*collectgarbage function
*_VERSION string
*warn function
*next function
*utf8 table
**offset function
**codepoint function
**len function
**codes function
**charpattern string
**char function
*math table
**maxinteger number
**sqrt function
**floor function
**abs function
**tan function
**deg function
**ceil function
**acos function
**asin function
**min function
**cosh function
**mininteger number
**tointeger function
**modf function
**ult function
**ldexp function
**log function
**randomseed function
**exp function
**frexp function
**pi number
**type function
**random function
**pow function
**atan2 function
**sinh function
**tanh function
**rad function
**log10 function
**cos function
**sin function
**max function
**atan function
**fmod function
**huge number
*pairs function
*error function
*load function
*rawset function
*ipairs function
*debug table
**getuservalue function
**getmetatable function
**getinfo function
**getlocal function
**gethook function
**upvaluejoin function
**traceback function
**setlocal function
**setuservalue function
**upvalueid function
**setupvalue function
**setcstacklimit function
**setmetatable function
**sethook function
**getupvalue function
*setmetatable function
*xpcall function
*_G table – its contents is not printed because it already is
*table table
**concat function
**insert function
**unpack function
**sort function
**move function
**pack function
**remove function
*rawlen function
*coroutine table
**close function
**resume function
**running function
**create function
**isyieldable function
**wrap function
**yield function
**status function

1 Like

Made your function more concise:

local function printTableAsTree(tbl, intents)
   for i, v in tbl do
	  local t = type(v)
      print(string.format("%s%s %s", intents, i, t))
		
	  if t == "table" and v ~= _G then -- no need for a new variable declaration 
         printTableAsTree(v, intents.."  ")
	  end
   end
end
printTableAsTree(_G, "")
2 Likes