博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lua非常有用的工具——递归打印表数据
阅读量:7080 次
发布时间:2019-06-28

本文共 3492 字,大约阅读时间需要 11 分钟。

Lua是一种非常小巧的语言。虽小,但五脏俱全。

在Lua中,我认为最最核心的数据结构就是表。表不仅可用作数组,还可以用作字典。Lua面向对象的实现也是用表实现的。

表对于Lua实在是太重要了!所以,在开发与调试过程中,让表可视化是非常迫切的需求。可Lua标准库里没有递归显示表中所有数据的函数。

于是,凭着对Lua的兴趣,我写了一个函数用于递归打印表中所有的数据。

废话不多话,如下为源代码:

 
--------------------------------------------------------------------- This library defines table_print(tb), table_tostring(tb) for -- printing table data-- -- Author: Hevake Lee-- Email : hevake_lcj@126.com-------------------------------------------------------------------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 .. "
= " 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 retend--------------------------------------------------------------------- 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 retendfunction 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_1local 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:

该库对外只提供了3个函数:

  • table_print(table_name)        递归打印表中的所有数据

  • table_tostring(table_name)   将表转换成字符串

  • table_lib_test()     模块自测函数(测试用的)

将该源码保存在Lua的库路径下的叫 table_lib.lua 文件里。在命令终端运行 lua

我们可以自己试一试:

 
> require "table_lib"> 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}>

就是这效果。

再试试打印 _G 表的内容,会有意想不到的惊喜。

 
> table_print(_G)

当然,目前还不够完善,只能说是将就用,还有很多地方需要改进的。希望大家多多提意见。

转载地址:http://otjml.baihongyu.com/

你可能感兴趣的文章
#ReactApp项目构建流程【3】
查看>>
canal 1.0.25 快速启动配置
查看>>
SpringBoot使用AOP+注解实现简单的权限验证
查看>>
Android 8.0 系统和API的变化
查看>>
Git 多人协作开发流程
查看>>
js 时间对象的常规操作
查看>>
BiuJS[v1.0]说明文档(2):数据劫持
查看>>
Centos 7 Yum方式安装Mongdb 3.4
查看>>
遇见大数据可视化 : 【云图】让数据可见
查看>>
Mac Docker 创建第一个Django 应用,Part 1
查看>>
zendAPI 的 CMake 参数详解
查看>>
【201天】黑马程序员27天视频学习笔记【Day18复习脑图】
查看>>
vue+webpack搭建单文件应用和多文件应用webpack.config.js的写法区别
查看>>
【Java系列】从字节码角度深度理解Java函数调用传参方式
查看>>
leetcode82. Remove Duplicates from Sorted List II
查看>>
简单学习node微信开发
查看>>
使用vue实现tab操作
查看>>
vim 编辑器简介
查看>>
LDAP开发学习
查看>>
JavaScript——作用域、变量声明提升、局部变量混谈
查看>>