首页 > 代码库 > lua部分 tips
lua部分 tips
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 = 1 local nSplitIndex = 1 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 - 1) nFindStartIndex = nFindLastIndex + string.len(szSeparator) nSplitIndex = nSplitIndex + 1 end return nSplitArray end
遍历lua数组
方法一,可以用for来遍历:[cpp] view plaincopy在CODE上查看代码片派生到我的代码片do table_week = { "w", "e", "r", "t", "y", "u", "i", } for i = 1, #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")
lua部分 tips
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。