function PrintTable(tb) for k,v in pairs(tb) do print(v) end print("-------------------") end function PrintTable2(tb) for i,v in ipairs(tb) do print(v) end print("-------------------") end ---------------------------------------------
function printT( ... ) for i,v in ipairs(...) do print(i,v) end end t1={} t2={} t3={} table.insert(t1,"t1") table.insert(t1,"t2") ,"t3") print "1 table.insert-----------" --printT(t1) print "2 table.concat-----
当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: for key, value in pairs(tbtest) do XXX end for key, value in ipairs(tbtest) do XXX end for i=1, #(tbtest) do XXX end for i=1, table.maxn(tbtest) do XXX 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. 此接口不是统计表中元素的
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