先上代码 function luautil.serialize(t, sort_parent, sort_child) local mark={} local assign={} local function ser_table(tbl,parent) mark[tbl]=parent local tmp={} local sortList = {}; for k,v in pairs(tbl) do sortList[#sortList + 1] = {key=k, value=v}; end
table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.) 返回表中最大的正数值index. 说明: 1. 此接口不是统计表中元素的
为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconcat = table.concat local tinsert = table.insert local srep = string.rep local type = type local pairs = pairs local tostring = tostring local next = nex
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} will initialize days[1] with the string "Sunday" (the first element has always index 1, n
官方手册里早已经给了答案,那就是靠lua内置的next函数 即如此用: a = {} if next(a) == nil then next其实就是pairs遍历table时用来取下一个内容的函数. 但是如果 a= nil 就会报错,所以还要先判断一下 a是否为nil. 于是封装后判断的lua table是否为空的函数如下: function tableIsEmpty(t) if t == nil then return true end return _G.next(t) == nilend