function split(s, delim)
local start = 1 local t = {}
while true do
local pos = string.find (s, delim, start, true)
if not pos then
break
end
table.insert (t, string.sub (s, start, pos - 1))
start = pos + string.len (delim)
end
table.insert (t, string.sub (s, start))
return t
end
function getTableLen(input)
local ret = 0
for i, v in pairs(input) do
ret = ret + 1
end
return ret
end
function random_table(input, count)
local temp = {}
for k,v in pairs(input) do
table.insert(temp, v)
end
local selected={}
math.randomseed(os.time())
if #input<=count then return temp end
while #selected < count do
local bingo = math.random(#temp)
table.insert(selected,table.remove(temp, bingo))
end
return selected
end
本文介绍了一个用于字符串分割的Lua函数及如何实现从表中随机选取指定数量元素的方法。通过这两个实用函数,读者可以学习到Lua语言中字符串操作和表处理的基本技巧。
798

被折叠的 条评论
为什么被折叠?



