一.打印table

 function PrintTable(tb)
if type(tb) ~= "table" then
print(tb)
return
end local level =
local content = "" local function GetSpace(level)
local spaceStr = ""
for i=,level do
spaceStr = spaceStr .. " "
end
return spaceStr
end local function GetString(value)
if type(value) ~= "string" then
return tostring(value)
else
return "\"" .. tostring(value) .. "\""
end
end local function PrintTb(tb, level)
level = level + for k,v in pairs(tb) do
if type(k) == "table" then
content = content .. "\n" .. GetSpace(level) .. "{"
PrintTb(k, level)
content = content .. "\n" .. GetSpace(level) .. "} = "
else
content = content .. "\n" .. GetSpace(level) .. GetString(k) .. " = "
end if type(v) == "table" then
content = content .. "{"
PrintTb(v, level)
content = content .. "\n" .. GetSpace(level) .. "}"
else
content = content .. GetString(v)
end
end
end PrintTb(tb, level)
print("{" .. content .. "\n}")
end

测试1(嵌套table):

 local x =
{
{
a = {
b =
},
"asd",
},
"",
{
["c"] = ,
[] = ,
},
}

输出:

测试2(key和value都是table):

 local a = {, [] = , ""}
local b = {a1 = "a1"}
local c = {}
c[a] =
c[b] = {b1 = }

输出:

二.复制table

 function CloneTable(tb)
local clonedTable = {} --记录复制过的table,防止无限递归
local function CloneTb(tb)
if type(tb) ~= "table" then
return tb
elseif clonedTable[tb] then
return clonedTable[tb]
end local newTb = {}
clonedTable[tb] = newTb
for key,value in pairs(tb) do
newTb[CloneTb(key)] = CloneTb(value)
end
return setmetatable(newTb, getmetatable(tb))
end
return CloneTb(tb)
end

测试:

 local a = {, {a1 = "a1"}}
local b = CloneTable(a)
a[] = ""
a[].a1 =
b[] = ""
PrintTable(a)
PrintTable(b)

输出:

[Lua]table(一):打印与复制的更多相关文章

  1. 树形打印lua table表

    为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...

  2. lua table integer index 特性

    table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...

  3. lua table 排序--满足多条件排序

    前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...

  4. cocos2d-x lua table数据存储

    cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...

  5. cocos2d-x lua table与json的转换

    cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...

  6. Lua table使用

    days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...

  7. lua table表

    lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...

  8. lua table表判断是否为空

    官方手册里早已经给了答案,那就是靠lua内置的next函数 即如此用: a = {} if next(a) == nil then next其实就是pairs遍历table时用来取下一个内容的函数. ...

  9. 关于 lua table表存储函数且运用

    --table 是lua的一种数据结构用来帮助我们创建不同的数据类型.如:数组和字典--lua table 使用关联型数组,你可以用任意类型的值来做数组的索引,但这个值不能是nil--lua tabl ...

  10. lua table排序报错与解决

    lua table排序 table的sort函数 比如按照大小进行排序,下面这种写法在某些情况下可能会排序错误,甚至报invalid order function for sorting table. ...

随机推荐

  1. ionic platform add ios, Error:spawn EACCES

    RT: cordova ionic 环境搭建好之后,需要添加平台才能打包,添加平台如果出错:Error:spawn EACCES, 原因是因为没添加hooks, 请使用 ionic add hooks ...

  2. Vivado HLS初识---阅读《vivado design suite tutorial-high-level synthesis》(3)

    Vivado HLS初识---阅读<vivado design suite tutorial-high-level synthesis>(3) 优化lab1 1.创建工程,开启HLS 运行 ...

  3. 协程实现多并发socket,跟NGINX一样

    server: #!/usr/bin/env python # -*- coding: utf-8 -*- # author aliex-hrg import gevent from gevent i ...

  4. idea下的调试配置

    react和ts的整合 https://github.com/Microsoft/TypeScript-React-Starter vue的 https://github.com/ducksoupde ...

  5. 黄聪: bootstrap 多模态框实现

    默认情况下,bootstrap模态框是不支持多个覆盖的,下面是一个解决办法(本人亲测), 将下面的代码复制到当前需要多个模态框的页面,问题就可以解决 $(document).on('show.bs.m ...

  6. 两种方式:mysql查看正在执行的sql语句

    mysql查看正在执行的sql语句 2015年08月21日 17:32:59 阅读数:15398   有2个方法: 1.使用processlist,但是有个弊端,就是只能查看正在执行的sql语句,对应 ...

  7. 模拟a标签click,弹出新页面

    $("<a>").attr("href", url).attr("target", "_blank")[0] ...

  8. 在windows 7中vagrant up 无反应,没任何信息输出

    本文转载自:https://blog.csdn.net/cow66/article/details/77993908 我的系统是windows 7 安装了vagrant,当运行vagrant up时, ...

  9. 【剑指offer】输出链表倒数第K个元素

    /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ ...

  10. C#对Mongodb数组对象操作

    Mongo对数据的存储非常随意,需要修改对象中的数组对象时,就会变得比较复杂. 类中的类对象可以直接通过“.”例如:Department.User.name 类中的对象User是数组时可以用Depar ...