local aa = {{a=11},{a=44},{a=33},{a=2} } table.sort(aa,function(a,b) return a.a>b.a end) for k, v in pairs(aa[1]) do ngx.say(k) ngx.say(v) end for k, v in pairs(aa[2]) do ngx.say(k) ngx.say(v) end for k, v in pairs(aa[3]) do ngx.say(k) ngx.say(v) end
当我在工作中使用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 前两种是泛型遍历,后两种是数值型遍历.当然你还
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-----
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