码迷,mamicode.com
首页 > 其他好文 > 详细

lua部分 tips

时间:2015-01-09 17:10:24      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/howeho/p/4213695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!