游戏人生
首页
(current)
GameDevTools
登陆
|
注册
个人中心
注销
Lua 教程
Lua 教程
Lua 编辑器安装
Lua 变量
Lua 数据类型
Lua 循环
Lua 流程控制
Lua 函数
Lua 运算符
Lua 字符串
Lua 数组
Lua 迭代器
Lua table(表)
Lua 模块(module)
Lua 文法 BNF范式
Lua 实例
<< Lua 迭代器
Lua 模块(module) >>
Lua table(表)
table是lua的一种类型。 在lua中,table是用的最频繁的一种数据结构,并不是因为它长得好看,而是因为lua只有着一种数据结构。我们用table来实现数组、字典、List、Queue等等。 ------------ ####lua table实例 ```lua ---table作为数组 local tmpHeroNameArray={"HeroA","HeroB","HeroC"} print("table as arrya >> tmpHeroNameArray[1]:" .. tmpHeroNameArray[1]) ---table作为字典 local tmpHeroInfoDic= { ["HeroA"]=1, ["HeroB"]=2, ["HeroC"]=3, } print("table as dictionary >> tmpHeroInfoDic[\"HeroA\"]:" .. tmpHeroInfoDic["HeroA"]) ``` 输出结果 table as arrya >> tmpHeroNameArray[1]:HeroA table as dictionary >> tmpHeroInfoDic["HeroA"]:1 ------------ ####lua table 插入数据 #####对于字典类型,直接赋值即可 ````lua ---table作为字典 local tmpHeroInfoDic= { ["HeroA"]=1, ["HeroB"]=2, ["HeroC"]=3, } print("table as dictionary >> tmpHeroInfoDic[\"HeroA\"]:" .. tmpHeroInfoDic["HeroA"]) ---table作为字段,新增数据 tmpHeroInfoDic["HeroD"]=4 print("table as dictionary >> tmpHeroInfoDic[\"HeroD\"]:" .. tmpHeroInfoDic["HeroD"]) ```` 输出结果: table as dictionary >> tmpHeroInfoDic["HeroA"]:1 table as dictionary >> tmpHeroInfoDic["HeroD"]:4 #####对于数组类型,使用table.insert ```lua ---不用管这个函数 ---打印table内容,注意 不能打印class function table.tostring ( varTable ) if varTable==nil then return "nil" end local tmpData={} for key,value in pairs(varTable) do if type(value)=="table" then value=table.tostring(value) end table.insert(tmpData,string.format("%s=%s[%s]",key,value,type(value))) end return(string.format("{%s}",table.concat(tmpData,","))) end ---------------------------------------------------- local tmpHeroNameArray={"HeroA","HeroB","HeroC"} print("before >> " .. table.tostring(tmpHeroNameArray)) table.insert(tmpHeroNameArray,1,"xx") print("after >> " .. table.tostring(tmpHeroNameArray)) ``` 输出结果: before >> {1=HeroA[string],2=HeroB[string],3=HeroC[string]} after >> {1=xx[string],2=HeroA[string],3=HeroB[string],4=HeroC[string]} ------------ ####lua table 删除数据 #####对于字典类型,直接设置为nil ```lua ---table作为字典 local tmpHeroInfoDic= { ["HeroA"]=1, ["HeroB"]=2, ["HeroC"]=3, } print("table as dictionary >> tmpHeroInfoDic[\"HeroA\"]:" .. tmpHeroInfoDic["HeroA"]) ---table作为字段,新增数据 tmpHeroInfoDic["HeroA"]=nil print("table as dictionary >> tmpHeroInfoDic[\"HeroA\"]:" .. tostring(tmpHeroInfoDic["HeroA"])) ``` 输出结果: table as dictionary >> tmpHeroInfoDic["HeroA"]:1 table as dictionary >> tmpHeroInfoDic["HeroA"]:nil #####对于数组类型,使用table.remove ```lua ---不用管这个函数,这只是用来打印table内容的 ---打印table内容,注意 不能打印class function table.tostring ( varTable ) if varTable==nil then return "nil" end local tmpData={} for key,value in pairs(varTable) do if type(value)=="table" then value=table.tostring(value) end table.insert(tmpData,string.format("%s=%s[%s]",key,value,type(value))) end return(string.format("{%s}",table.concat(tmpData,","))) end ---------------------------------------------------- local tmpHeroNameArray={"HeroA","HeroB","HeroC"} print("before >> " .. table.tostring(tmpHeroNameArray)) table.remove(tmpHeroNameArray,1) print("after >> " .. table.tostring(tmpHeroNameArray)) ``` 输出结果: before >> {1=HeroA[string],2=HeroB[string],3=HeroC[string]} after >> {1=HeroB[string],2=HeroC[string]} ------------ #### 将table中数据,拼接为一个字符串 ```lua ---table拼接字符串 local tmpStrArray={"make ","game ","dev ","simple ","in www.thisisgame.com.cn"} local tmpStrConcat=table.concat(tmpStrArray) print(tmpStrConcat) ``` 输出结果: make game dev simple in www.thisisgame.com.cn ------------ ####lua table 排序 ```lua ---table 排序 local tmpStrArray={"make ","game ","dev ","simple ","in www.thisisgame.com.cn"} table.sort(tmpStrArray) ---输出排序后的 for _,value in pairs(tmpStrArray) do print(value) end ``` 输出结果: dev game in www.thisisgame.com.cn make simple ------------ ####lua table api 下面表中列出了 lua table的api: |方法 & 用途 | :------------ | :------------ | |table.concat (table [, sep [, start [, end]]]):<br><br>table.concat()函数将 从start位置到end位置的所有元素拼接成字符串, 元素间以指定的分隔符(sep)隔开。<br> |table.insert (table, [pos,] value):<br><br>在table的数组部分指定位置(pos)插入值为value的一个元素. pos参数可选, 默认为数组部分末尾.<br> |table.remove (table [, pos])<br><br>返回table数组部分位于pos位置的元素. 后面元素前移. pos参数可选, 默认为table长度, 即从最后一个元素删起。<br> |table.sort (table [, comp])<br><br>进行升序排序。 
<< Lua 迭代器
Lua 模块(module) >>
提交
5e4f8a232de88f4e9f9a45ff