--****************************************************************************** --* * --* File: Lua_01.lua Revision: 1.0 * --* * --* Purpose: lists all (predefined) global variables in Lua 4.0 * --* * --* Creation: 28.02.2002 Last Modification: 28.02.2002 * --* * --* Platform: IBM-compatible PC running Windows 98SE * --* * --* Environment: Lua 4.0, TkLua 4.0 * --* * --* Author: Andreas Rozek Phone: ++49 (711) 6770682 * --* Kirschblütenweg 15 Fax: - * --* D-70569 Stuttgart EMail: Andreas.Rozek@T-Online.De * --* Germany * --* * --* URL: http://www.Andreas-Rozek.de/ * --* * --* Copyright: the software is published under the "GNU Lesser General Pub- * --* lic License" (see "http://www.fsf.org/copyleft/lesser.html" * --* for additional information) * --* * --* Comments: (none) * --* * --****************************************************************************** --****************************************************************************** --* * --* valueOf converts a given (global) value into a string * --* * --****************************************************************************** function valueOf (Value) local ValueType = type(Value); if (ValueType == "nil") then return "nil"; end; if (ValueType == "number") then return tostring(Value); end; if (ValueType == "string") then if (strlen(Value) <= 50) then return '"'..Value..'"'; else return '"'..strsub(Value,1,50-3)..'..."'; end; end; return "("..ValueType..")";-- ValueType in {"function", "userdata", "table"} end; --****************************************************************************** --* * --* main program * --* * --****************************************************************************** print(); print("Lua_01 - lists all (predefined) global variables in Lua 4.0"); print(); local GlobalList = globals(); local VarName = next(GlobalList,nil); if (VarName == nil) then print("no local variables found"); else print("list of global variables:"); --**** sort list of globals by name and determine length of longest global name **** local NameList = {}; local maxNameLength = 0; while (VarName ~= nil) do tinsert(NameList,VarName); maxNameLength = max(maxNameLength,strlen(VarName)); VarName = next(GlobalList,VarName); end; maxNameLength = min(maxNameLength,20-2); -- sort(NameList); -- simply uses character codes for comparison sort(NameList, function (a,b) return strlower(a) < strlower(b); end); --**** now print the contents of every global variable (if possible) **** local i; for i = 1,getn(NameList) do local VarOutName; VarName = NameList[i]; -- just an abbreviation if (strlen(VarName) <= maxNameLength) then VarOutName = '"'..VarName..'"'; VarOutName = strrep(" ",maxNameLength-strlen(VarName))..VarOutName; else VarOutName = '"'..strsub(VarName,1,maxNameLength-3)..'..."'; end; print(VarOutName..": "..valueOf(getglobal(VarName))); Index,VarName = next(NameList,Index); end; end; exit();