1.一个示例实现列表的元素的求和
[root@]# more funcAdd.lua
function add(a)
local sum = 0
for i = 1,#a do
sum = sum + a[i]
end
return sum
end
a = {1,2,3,4,5,6}
local sum = add(a)
print(sum)
文章来源:https://www.toymoban.com/news/detail-679546.html
2.判断json串的元素对应的值是列表,判断列表中的元素是否为0的方法文章来源地址https://www.toymoban.com/news/detail-679546.html
package.cpath = "cjson/?.so;" .. package.cpath
local body = "{\"code\":\"000000\",\"data\":\[\]}"
local cjson = require "cjson"
local cjson1 = cjson.new()
local ev = {}
ev = cjson1.decode(body)
local datalist = ev["data"]
if type(ev.data) == "table" and #ev.data == 0 then
print('datalist has 0 element')
else
print('datalist has more than 0 elements')
end
到了这里,关于lua的函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!