lua 判断table数据元素的数量
发布日期:2021-11-02 06:36:31 浏览次数:19 分类:技术文章

本文共 1221 字,大约阅读时间需要 4 分钟。

记录:lua 判断table数据元素的数量:#table

示例:

local mysql = require "resty.mysql"local db, err = mysql:new()if not db then    ngx.say("failed to instantiate mysql: ", err)    returnenddb:set_timeout(1000) -- 1 seclocal ok, err, errno, sqlstate = db:connect{
host = "", port = 3306, database = "", user = "", password = "", max_packet_size = 1024 * 1024 }if not ok then ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate) returnendngx.say("connected to mysql.")-- 检查是否存在要生成的cors配置 configlocal select_sql = "select * from config where name='"..name.."' order by createtime asc"local corsconfig,err,errno,sqlstate = db:query(select_sql)if not corsconfig then ngx.say("select rows error:",err,",errno:",errno,",sqlstate:",sqlstate) breakend-- 判断corsconfig(table数据)元素的数量:#corsconfigif #corsconfig ~= 0 then -- 循环此cdn服务下的所有CORS配置 for j,corsrow in ipairs(corsconfig) do -- 调用函数:生成单个cors配置文件 createCorsConf(corsrow,name) endend

但这种方法要慎用,使用过程中,遇到过这种bug:

local t = {
"a", "a", "b", "b","c"} -- 重复的 a 会被过滤掉 LuaPrintTable(t,'table') ngx.print(#t) -- 打印结果:5 -- 过滤掉重复的元素 local n = uniqueTable(t) LuaPrintTable(n,'table') ngx.print(#n) -- 打印结果:1 do return end

打印图示:

在这里插入图片描述

转载地址:https://blog.csdn.net/weixin_43652106/article/details/108596155 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:TP6.0记录文档
下一篇:nginx CORS 跨域共享配置

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月15日 18时12分50秒