Site Notice
hello, world
Difference between revisions of "Module:Util"
From Project-EPB Commons
([InPageEdit] 没有编辑摘要) |
|||
Line 9: | Line 9: | ||
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 29: | ||
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 |
Revision as of 01:32, 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