Site Notice
hello, world
Difference between revisions of "Module:Util"
From Project-EPB Commons
([InPageEdit] 没有编辑摘要) |
([InPageEdit] 没有编辑摘要) |
||
Line 9: | Line 9: | ||
end | end | ||
return length | return length | ||
+ | end | ||
+ | |||
+ | -- 某table是否含有value | ||
+ | function p.hasValue(tbl, value) | ||
+ | if tbl == nil then | ||
+ | return false | ||
+ | end | ||
+ | |||
+ | for k, v in pairs(tbl) do | ||
+ | if v == value then | ||
+ | return true | ||
+ | end | ||
+ | end | ||
+ | return false | ||
end | end | ||
-- 返回表中含有value值的key | -- 返回表中含有value值的key | ||
− | function p. | + | function p.keyOfValue(tbl, value) |
local final = {} | local final = {} | ||
for k, v in pairs(tbl) do | for k, v in pairs(tbl) do |
Revision as of 01: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是否含有value
function p.hasValue(tbl, value)
if tbl == nil then
return false
end
for k, v in pairs(tbl) do
if v == 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