lua中的坑
在工作中使用lua也有一年了,代碼也寫了不少,踩過不少坑,這里記錄一下。
- table.sort
table.sort是lua自帶的排序函數,數據量小時,也還是不錯的。不過要注意你傳入的compare函數。例如:
local tb = { 9,8,3,777,0,36548,556,0 }table.sort( tb,function(a,b) return a>=b end )上面的代碼夠簡單了,但是你運行起來,卻是報錯了。
Program starting as '"D:\programs\ZeroBraneStudio\bin\lua53.exe" -e "io.stdout:setvbuf('no')" "D:\work_code\lua\Test\Test.lua"'. Program 'lua53.exe' started in 'D:\work_code\server' (pid: 6572). D:\programs\ZeroBraneStudio\bin\lua53.exe: D:\work_code\lua\Test\Test.lua:77: invalid order function for sorting stack traceback: [C]: in function 'sort' D:\work_code\lua\Test\Test.lua:77: in main chunk [C]: in ? Program completed in 0.04 seconds (pid: 6572).原因在于compare函數中不能出現等于,即a>b或a<b都OK,但不能是a>=b或a<=b。如果使用了=,當數組中出現兩個權重一樣的元素時,就會報錯。具體原因你可以看table.sort的官方文檔,有句話:
If comp is given, then it must be a function that receives two list elements and returns true when the first element must come before the second in the final order (so that, after the sort, i < j implies not comp(list[j],list[i]))如果是報invalid order function for sorting也就罷了,網上查一下就能找到答案。然而我在項目中的報錯卻比較奇怪:
function Society_city:do_player_hurt_sort()local player_hurt = {}for ty,hurt_info in pairs( self.player_hurt ) dofor pid,info in pairs( hurt_info ) dolocal _info = {}_info.ty = ty_info.pid = pid_info.hurt = info.hurt_info.times = info.timestable.insert( player_hurt,_info )endendtable.sort( player_hurt,function(a,b) return a.hurt >= b.hurt end )return player_hurt end報錯信息為:
society_city.lua:679: attempt to index local 'a' (a nil value)我一直以為是數組中加入了一個nil,但查了半天,原來是>=的原因。
- table編譯
在我們的項目中,策劃將配置填到excel表,然后用工具將excel表批量轉為lua表作為配置。這樣在lua中直接require就可以使用配置了。轉出來的配置表通常是這樣的:
local t = {[1] = {['id'] = 1,['icon'] = 1001,['name'] = '默認頭像框',['description'] = '初始贈送',['type_id'] = 1,['sort'] = 1,},[2] = {['id'] = 2,['icon'] = 1002,['name'] = '10級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 10,['sort'] = 2,},[3] = {['id'] = 3,['icon'] = 1003,['name'] = '20級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 20,['sort'] = 3,},[4] = {['id'] = 4,['icon'] = 1004,['name'] = '30級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 30,['sort'] = 4,},[5] = {['id'] = 5,['icon'] = 1005,['name'] = '40級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 40,['sort'] = 5,},[6] = {['id'] = 6,['icon'] = 1006,['name'] = '50級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 50,['sort'] = 6,},[7] = {['id'] = 7,['icon'] = 1007,['name'] = '60級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 60,['sort'] = 7,},} View Code在某些功能中,是需要將id從小到大遍歷的。由于策劃配置時總是嚴格按小到大的,轉換出來的配置表key值也是從小到大的。然而,當我們require這個table時,順序卻是亂的。即
for k,v in pairs( t ) do
print( k,v )
end
輸出:
Program 'lua.exe' started in 'D:\work_code\server' (pid: 8044). 2 table: 0x003d8ff8 5 table: 0x003d92c8 3 table: 0x003d90e8 7 table: 0x003d8a58 1 table: 0x003da560 4 table: 0x003d91d8 6 table: 0x003d92f0 Program completed in 0.03 seconds (pid: 8044).這樣的代碼,第一個取得的id并不一定是1。也就是說,lua虛擬機在編譯這個文件時,里面的元素并不是按順序的,即使key值是int型并且按從小到大并嚴格自增。
轉載于:https://www.cnblogs.com/coding-my-life/p/5223533.html
總結
- 上一篇: [转载]高质量c/c++编程指南读书笔记
- 下一篇: 获取某个日期是一年中的第几周