Lua是一种非常小巧的语言。虽小,但五脏俱全。
在Lua中,我认为最最核心的数据结构就是表。表不仅可用作数组,还可以用作字典。Lua面向对象的实现也是用表实现的。
表对于Lua实在是太重要了!所以,在开发与调试过程中,让表可视化是非常迫切的需求。可Lua标准库里没有递归显示表中所有数据的函数。
于是,凭着对Lua的兴趣,我写了一个函数用于递归打印表中所有的数据。
废话不多话,如下为源代码:
------------------------------------------------------------------- -- This library defines table_print(tb), table_tostring(tb) for -- printing table data -- -- Author: Hevake Lee -- Email : [email protected] ------------------------------------------------------------------- local function _list_table(tb, table_list, level) local ret = "" local indent = string.rep(" ", level*4) for k, v in pairs(tb) do local quo = type(k) == "string" and "\"" or "" ret = ret .. indent .. "[" .. quo .. tostring(k) .. quo .. "] = " if type(v) == "table" then local t_name = table_list[v] if t_name then ret = ret .. tostring(v) .. " -- > [\"" .. t_name .. "\"]\n" else table_list[v] = tostring(k) ret = ret .. "{\n" ret = ret .. _list_table(v, table_list, level+1) ret = ret .. indent .. "}\n" end elseif type(v) == "string" then ret = ret .. "\"" .. tostring(v) .. "\"\n" else ret = ret .. tostring(v) .. "\n" end end local mt = getmetatable(tb) if mt then ret = ret .. "\n" local t_name = table_list[mt] ret = ret .. indent .. "<metatable> = " if t_name then ret = ret .. tostring(mt) .. " -- > [\"" .. t_name .. "\"]\n" else ret = ret .. "{\n" ret = ret .. _list_table(mt, table_list, level+1) ret = ret .. indent .. "}\n" end end return ret end ------------------------------------------------------------------- -- Public functions ------------------------------------------------------------------- function table_tostring(tb) if type(tb) ~= "table" then error("Sorry, it‘s not table, it is " .. type(tb) .. ".") end local ret = " = {\n" local table_list = {} table_list[tb] = "root table" ret = ret .. _list_table(tb, table_list, 1) ret = ret .. "}" return ret end function table_print(tb) print(tostring(tb) .. table_tostring(tb)) end ------------------------------------------------------------------- -- For test ------------------------------------------------------------------- local test_table_2 = { print, } local test_table_1 = { 12, 22.5, true, info = { name = "Jack", age = 26, lifeexp = { ["1986"] = "Both", ["2013"] = "Work in Tencent", ["2015"] = "Get married" }, wife = "Lucy" }, "Hello test", recu_table = test_table_2, ["2"] = 13 } test_table_2.recu_table = test_table_1 local metatable = { __index = test_table_2, __add = function(a, b) return 0 end } setmetatable(test_table_1, metatable) function table_lib_test() table_print(test_table_1) end
也可以访问git:http://git.oschina.net/hevake_lcj/LuaScripts/blob/master/table_lib.lua
该库对外只提供了3个函数:
- table_print(table_name) 递归打印表中的所有数据
- table_tostring(table_name) 将表转换成字符串
- table_lib_test() 模块自测函数(测试用的)
将该源码保存在Lua的库路径下的叫 table_lib.lua 文件里。在命令终端运行 lua
我们可以自己试一试:
> my_table = {name = ‘Hevake Lee‘, age = 27, children = {‘Peter‘, ‘John‘}} > table_print(my_table) table: 0x8fc77c8 = { ["children"] = { [1] = "Peter" [2] = "John" } ["name"] = "Hevake Lee" ["age"] = 27 } >
就是这效果。
当然,目前还不够完善,只能说是将就用,还有很多地方需要改进的。希望大家多多提意见。
时间: 2024-11-06 23:46:28