lua部分 tips

时间:2023-03-08 16:33:07

lua文件刷新

function require_ex( _mname )
if _mname == "" then
return
end
if package.loaded[_mname] then
end
package.loaded[_mname] = nil
require( _mname )
end

lua字符串分割

function Split(szFullString, szSeparator)
local nFindStartIndex =
local nSplitIndex =
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - )
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex +
end
return nSplitArray
end 第二种
  1. function split(str, reps)
  2. local resultStrsList = {};
  3. string.gsub(str, '[^' .. reps ..']+', function(w) table.insert(resultStrsList, w) end );
  4. return resultStrsList;
  5. end
 

遍历lua数组

方法一,可以用for来遍历:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
do
table_week = {
"w",
"e",
"r",
"t",
"y",
"u",
"i",
} for i = , #table_week do
print(table_week[i])
end
end #后面接一个数组或者tabe来遍历它,i是该table或者数组的起始下标。
方法2:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
do
table_week = {
"w",
"e",
"r",
"t",
"y",
"u",
"i",
}
for i, v in pairs(table_week) do
print(i)
end
end 这种是采用迭代器的方式遍历的,i为下标,v为table或者数组的值。 方式3:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
do
table_week = {
"w",
"e",
"r",
"t",
"y",
"u",
"i",
}
for i in pairs(table_week) do
print(i);
end
end i为table或者数组的下标。 方式4:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
do
table_view = {
"w",
"e",
"r",
color1 = "red",
color2 = "blue",
{"a1", "a2", "a3"},
{"b1", "b2", "b3"},
{"c1", "c2", "c3"},
}
for i, v in pairs(table_view) do
if type(v) == "table" then
for new_table_index, new_table_value in pairs(v) do
print(new_table_value)
end
else
print(v)
end
end end 注:type(v)
功能:返回参数的类型名("nil","number", "string", "boolean", "table", "function", "thread", "userdata")