Site Notice
hello, world
Difference between revisions of "Module:Util"
From Project-EPB Commons
([InPageEdit] 没有编辑摘要) |
([InPageEdit] 没有编辑摘要) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
-- 常用Lua工具集 | -- 常用Lua工具集 | ||
+ | -- 部分模块摘自互联网 | ||
local p = {} | local p = {} | ||
Line 9: | Line 10: | ||
end | end | ||
return length | return length | ||
+ | end | ||
+ | |||
+ | -- 某table是否含有key | ||
+ | function p.hasKey(tbl, key) | ||
+ | if tbl == nil then | ||
+ | return false | ||
+ | end | ||
+ | for k, v in pairs(tbl) do | ||
+ | if k == key then | ||
+ | return true | ||
+ | end | ||
+ | end | ||
+ | return false | ||
end | end | ||
Line 16: | Line 30: | ||
return false | return false | ||
end | end | ||
− | |||
for k, v in pairs(tbl) do | for k, v in pairs(tbl) do | ||
− | if | + | if tbl[k] == value then |
return true | return true | ||
end | end |
Latest revision as of 02:16, 11 April 2020
Documentation for this module may be created at Module:Util/doc
-- 常用Lua工具集
-- 部分模块摘自互联网
local p = {}
-- 获取表长度
function p.tableLength(tbl)
local length = 0
for k, v in pairs(tbl) do
length = length + 1
end
return length
end
-- 某table是否含有key
function p.hasKey(tbl, key)
if tbl == nil then
return false
end
for k, v in pairs(tbl) do
if k == key then
return true
end
end
return false
end
-- 某table是否含有value
function p.hasValue(tbl, value)
if tbl == nil then
return false
end
for k, v in pairs(tbl) do
if tbl[k] == value then
return true
end
end
return false
end
-- 返回表中含有value值的key
function p.keyOfValue(tbl, value)
local final = {}
for k, v in pairs(tbl) do
if v == value then
final[k] = v
end
end
return final
end
return p