Anyone in PHP will know the print_rvar_export function in PHP, which can be easily used to print arrays or export variables. This function is not provided in Lua, but is often required in practical applications. Today, a simple function is encapsulated to implement the class function to print and export the table: -- dump. lua -- [dump object @ parammixedobj @ returnstr
Anyone in PHP will know the print_r/var_export function in PHP, which can be easily used to print arrays or export variables. This function is not provided in Lua, but is often required in practical applications. Today, a simple function is encapsulated to implement the class function to print/export the table: -- dump. lua -- [dump object @ param mixed obj @ return str
Anyone in PHP will know the print_r/var_export function in PHP, which can be easily used to print arrays or export variables. This function is not provided in Lua, but is often required in practical applications.
Today, a simple function is encapsulated to implement similar functions to print/export tables:
-- Dump. lua -- [[dump object @ param mixed obj @ return string] function debug. dump (obj) local getIndent, quoteStr, wrapKey, wrapVal, isArray, dumpObj getIndent = function (level) return string. rep ("\ t", level) end quoteStr = function (str) str = string. gsub (str, "[% c \"] ", {[" \ t "] =" \ t ", ["\ r"] = "\ r", ["\ n"] = "\ n", ["\" "] = "\\\"", ["\"] = "\\\\",}) return '"'.. str .. '"'end wrapKey = function (val) If type (val) = "number" then return "[".. val .. "]" elseif type (val) = "string" then return "[".. quoteStr (val ).. "]" else return "[".. tostring (val ).. "]" end wrapVal = function (val, level) if type (val) = "table" then return dumpObj (val, level) elseif type (val) = "number" then return val elseif type (val) = "string" then return quoteStr (val) else return tostring (val) end local IsArray = function (arr) local count = 0 for k, v in pairs (arr) do count = count + 1 end for I = 1, count do if arr [I] = nil then return false end return true, count end dumpObj = function (obj, level) if type (obj )~ = "Table" then return wrapVal (obj) end level = level + 1 local tokens = {} tokens [# tokens + 1] = "{" local ret, count = isArray (obj) if ret then for I = 1, count do tokens [# tokens + 1] = getIndent (level ).. wrapVal (obj [I], level ).. "," end else for k, v in pairs (obj) do tokens [# tokens + 1] = getIndent (level ).. wrapKey (k ).. "= ".. wrapVal (v, level ).. "," end tokens [# tokens + 1] = getIndent (level-1 ).. "}" return table. concat (tokens, "\ n") end return dumpObj (obj, 0) end
Test code:
-- test.lualocal obj = { string1 = "Hi! My name is LiXianlin", string2 = "aa\tbb\rcc\ndd\\ee\"ff", string3 = "a\\tb\\rc\\n\\\\ee\"ff", int = 9527, float = 3.1415, bool = true, array = { 1, 2, 3, { a = 21, b = 22, c = 23, }, }, table = { x = 100, y = 200, w = 960, h = 640, }, [88] = 88888, [9.7] = 22222,}print(debug.dump(obj))
Output result:
{ ["string1"] = "Hi! My name is LiXianlin", [9.7] = 22222, ["table"] = { ["y"] = 200, ["x"] = 100, ["h"] = 640, ["w"] = 960, }, [88] = 88888, ["string2"] = "aa\tbb\rcc\ndd\\ee\"ff", ["int"] = 9527, ["string3"] = "a\\tb\\rc\\n\\\\ee\"ff", ["bool"] = true, ["array"] = { 1, 2, 3, { ["b"] = 22, ["a"] = 21, ["c"] = 23, }, }, ["float"] = 3.1415,}
Function features:
1. Theoretically supporting infinite table nesting
2. format the output and make it readable.
3. The output result can be directly used for lua code.
4. function, userdata, thread type tostring output
5. When a string contains control characters, it may be affected (only \ t, \ r, \ n, etc. are processed in the Code)