如何将表转储到控制台?

我无法显示包含嵌套表(n-deep)的表的内容。 我想通过print语句或快速和肮脏的东西转储出或控制台,但我不知道如何。 我正在寻找与使用gdb打印NSDictionary时所得到的相当的结果。

随意浏览桌面序列化的Lua Wiki 。 它列出了如何将表转储到控制台的几种方法。

你只需要select最适合你的一个。 有很多方法可以做到这一点,但我通常最终会使用Penlight中的那个 :

 > t = { a = { b = { c = "Hello world!", 1 }, 2, d = { 3 } } } > require 'pl.pretty'.dump(t) { a = { d = { 3 }, b = { c = "Hello world!", 1 }, 2 } } 

我知道这个问题已经被标记为回答,但让我插入我自己的图书馆在这里。 这就是所谓的inspect.lua,你可以在这里find它:

https://github.com/kikito/inspect.lua

这只是一个文件,你可以从任何其他文件要求。 它返回一个函数,将任何Lua值转换为可读的string:

 local inspect = require('inspect') print(inspect({1,2,3})) -- {1, 2, 3} print(inspect({a=1,b=2}) -- { -- a = 1 -- b = 2 -- } 

它正确缩进子表,并正确处理“recursion表”(包含对自身的引用的表),所以它不会陷入无限循环。 它以合理的方式分类价值。 它也打印metatable信息。

问候!

我发现这个有用。 因为如果recursion它也可以打印嵌套表。

 function dump(o) if type(o) == 'table' then local s = '{ ' for k,v in pairs(o) do if type(k) ~= 'number' then k = '"'..k..'"' end s = s .. '['..k..'] = ' .. dump(v) .. ',' end return s .. '} ' else return tostring(o) end end 

例如

 local people = { { name = "Fred", address = "16 Long Street", phone = "123456" }, { name = "Wilma", address = "16 Long Street", phone = "123456" }, { name = "Barney", address = "17 Long Street", phone = "123457" } } print("People:", dump(people)) 

生成以下输出:

人们:{[1] = {[“address”] = 16 Long Street,[“phone”] = 123456,[“name”] = Fred,},[2] = {[“address”] = ,[“phone”] = 123456,[“name”] = Wilma,},[3] = {[“address”] = 17 Long Street,[“phone”] = 123457,[“name” },}

发现这个:

 -- Print contents of `tbl`, with indentation. -- `indent` sets the initial level of indentation. function tprint (tbl, indent) if not indent then indent = 0 end for k, v in pairs(tbl) do formatting = string.rep(" ", indent) .. k .. ": " if type(v) == "table" then print(formatting) tprint(v, indent+1) elseif type(v) == 'boolean' then print(formatting .. tostring(v)) else print(formatting .. v) end end end 

从这里https://gist.github.com/ripter/4270799

对我来说工作很好…

我所见过的大多数纯粹的lua打印表函数都有深度recursion的问题,并且在深度过大时往往会导致堆栈溢出。 这个我写的打印表函数没有这个问题。 由于处理串联的方式,它也应该能够处理真正的大表。 在我个人使用这个function的时候,它在大约一秒钟内输出了63k行文件。

输出还保留lua语法,通过将输出写入文件(如果修改为仅允许数字,布尔值,string和表格数据types进行格式化),可以轻松修改脚本以实现简单的持久性存储。

 function print_table(node) -- to make output beautiful local function tab(amt) local str = "" for i=1,amt do str = str .. "\t" end return str end local cache, stack, output = {},{},{} local depth = 1 local output_str = "{\n" while true do local size = 0 for k,v in pairs(node) do size = size + 1 end local cur_index = 1 for k,v in pairs(node) do if (cache[node] == nil) or (cur_index >= cache[node]) then if (string.find(output_str,"}",output_str:len())) then output_str = output_str .. ",\n" elseif not (string.find(output_str,"\n",output_str:len())) then output_str = output_str .. "\n" end -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings table.insert(output,output_str) output_str = "" local key if (type(k) == "number" or type(k) == "boolean") then key = "["..tostring(k).."]" else key = "['"..tostring(k).."']" end if (type(v) == "number" or type(v) == "boolean") then output_str = output_str .. tab(depth) .. key .. " = "..tostring(v) elseif (type(v) == "table") then output_str = output_str .. tab(depth) .. key .. " = {\n" table.insert(stack,node) table.insert(stack,v) cache[node] = cur_index+1 break else output_str = output_str .. tab(depth) .. key .. " = '"..tostring(v).."'" end if (cur_index == size) then output_str = output_str .. "\n" .. tab(depth-1) .. "}" else output_str = output_str .. "," end else -- close the table if (cur_index == size) then output_str = output_str .. "\n" .. tab(depth-1) .. "}" end end cur_index = cur_index + 1 end if (size == 0) then output_str = output_str .. "\n" .. tab(depth-1) .. "}" end if (#stack > 0) then node = stack[#stack] stack[#stack] = nil depth = cache[node] == nil and depth + 1 or depth - 1 else break end end -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings table.insert(output,output_str) output_str = table.concat(output) print(output_str) end 

这里是一个例子:

 local t = { ["abe"] = {1,2,3,4,5}, "string1", 50, ["depth1"] = { ["depth2"] = { ["depth3"] = { ["depth4"] = { ["depth5"] = { ["depth6"] = { ["depth7"]= { ["depth8"] = { ["depth9"] = { ["depth10"] = {1000}, 900}, 800},700},600},500}, 400 }, 300}, 200}, 100}, ["ted"] = {true,false,"some text"}, "string2", [function() return end] = function() return end, 75 } print_table(t) 

输出:

 { [1] = 'string1', [2] = 50, [3] = 'string2', [4] = 75, ['abe'] = { [1] = 1, [2] = 2, [3] = 3, [4] = 4, [5] = 5 }, ['function: 06472B70'] = 'function: 06472A98', ['depth1'] = { [1] = 100, ['depth2'] = { [1] = 200, ['depth3'] = { [1] = 300, ['depth4'] = { [1] = 400, ['depth5'] = { [1] = 500, ['depth6'] = { [1] = 600, ['depth7'] = { [1] = 700, ['depth8'] = { [1] = 800, ['depth9'] = { [1] = 900, ['depth10'] = { [1] = 1000 } } } } } } } } } }, ['ted'] = { [1] = true, [2] = false, [3] = 'some text' } } 

table.tostring table.tostring metehod实际上是非常完整的。 它涉及嵌套表格,缩进级别是可变的,…请参阅https://github.com/fab13n/metalua/blob/master/src/lib/metalua/table2.lua

这是我的版本,支持排除表和userdata

 -- Lua Table View by Elertan table.print = function(t, exclusions) local nests = 0 if not exclusions then exclusions = {} end local recurse = function(t, recurse, exclusions) indent = function() for i = 1, nests do io.write(" ") end end local excluded = function(key) for k,v in pairs(exclusions) do if v == key then return true end end return false end local isFirst = true for k,v in pairs(t) do if isFirst then indent() print("|") isFirst = false end if type(v) == "table" and not excluded(k) then indent() print("|-> "..k..": "..type(v)) nests = nests + 1 recurse(v, recurse, exclusions) elseif excluded(k) then indent() print("|-> "..k..": "..type(v)) elseif type(v) == "userdata" or type(v) == "function" then indent() print("|-> "..k..": "..type(v)) elseif type(v) == "string" then indent() print("|-> "..k..": ".."\""..v.."\"") else indent() print("|-> "..k..": "..v) end end nests = nests - 1 end nests = 0 print("### START TABLE ###") for k,v in pairs(t) do print("root") if type(v) == "table" then print("|-> "..k..": "..type(v)) nests = nests + 1 recurse(v, recurse, exclusions) elseif type(v) == "userdata" or type(v) == "function" then print("|-> "..k..": "..type(v)) elseif type(v) == "string" then print("|-> "..k..": ".."\""..v.."\"") else print("|-> "..k..": "..v) end end print("### END TABLE ###") end 

这是一个例子

 t = { location = { x = 10, y = 20 }, size = { width = 100000000, height = 1000, }, name = "Sidney", test = { hi = "lol", }, anotherone = { 1, 2, 3 } } table.print(t, { "test" }) 

打印:

  ### START TABLE ### root |-> size: table | |-> height: 1000 |-> width: 100000000 root |-> location: table | |-> y: 20 |-> x: 10 root |-> anotherone: table | |-> 1: 1 |-> 2: 2 |-> 3: 3 root |-> test: table | |-> hi: "lol" root |-> name: "Sidney" ### END TABLE ### 

注意,根不会排除排除

你必须自己编码,恐怕。 我写了这个,可能对你有些用处

 function printtable(table, indent) indent = indent or 0; local keys = {}; for k in pairs(table) do keys[#keys+1] = k; table.sort(keys, function(a, b) local ta, tb = type(a), type(b); if (ta ~= tb) then return ta < tb; else return a < b; end end); end print(string.rep(' ', indent)..'{'); indent = indent + 1; for k, v in pairs(table) do local key = k; if (type(key) == 'string') then if not (string.match(key, '^[A-Za-z_][0-9A-Za-z_]*$')) then key = "['"..key.."']"; end elseif (type(key) == 'number') then key = "["..key.."]"; end if (type(v) == 'table') then if (next(v)) then printf("%s%s =", string.rep(' ', indent), tostring(key)); printtable(v, indent); else printf("%s%s = {},", string.rep(' ', indent), tostring(key)); end elseif (type(v) == 'string') then printf("%s%s = %s,", string.rep(' ', indent), tostring(key), "'"..v.."'"); else printf("%s%s = %s,", string.rep(' ', indent), tostring(key), tostring(v)); end end indent = indent - 1; print(string.rep(' ', indent)..'}'); end 
 --~ print a table function printTable(list, i) local listString = '' --~ begin of the list so write the { if not i then listString = listString .. '{' end i = i or 1 local element = list[i] --~ it may be the end of the list if not element then return listString .. '}' end --~ if the element is a list too call it recursively if(type(element) == 'table') then listString = listString .. printTable(element) else listString = listString .. element end return listString .. ', ' .. printTable(list, i + 1) end local table = {1, 2, 3, 4, 5, {'a', 'b'}, {'G', 'F'}} print(printTable(table)) 

嗨,我写了一个在纯Lua中做这个的siple代码,它有一个bug(在列表的最后一个元素之后写一个昏迷),但是我如何快速地将它作为原型写下来,我会让它适应你的需要。