lua中的坑
在工作中使用lua也有一年了,代码也写了不少,踩过不少坑,这里记录一下。
- table.sort
table.sort是lua自带的排序函数,数据量小时,也还是不错的。不过要注意你传入的compare函数。例如:
local tb = { ,,,,,,, }
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: ).
D:\programs\ZeroBraneStudio\bin\lua53.exe: D:\work_code\lua\Test\Test.lua:: invalid order function for sorting
stack traceback:
[C]: in function 'sort'
D:\work_code\lua\Test\Test.lua:: in main chunk
[C]: in ?
Program completed in 0.04 seconds (pid: ).
原因在于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 ) do
for pid,info in pairs( hurt_info ) do
local _info = {}
_info.ty = ty
_info.pid = pid
_info.hurt = info.hurt
_info.times = info.times table.insert( player_hurt,_info )
end
end table.sort( player_hurt,function(a,b) return a.hurt >= b.hurt end ) return player_hurt
end
报错信息为:
society_city.lua:: attempt to index local 'a' (a nil value)
我一直以为是数组中加入了一个nil,但查了半天,原来是>=的原因。
- table编译
在我们的项目中,策划将配置填到excel表,然后用工具将excel表批量转为lua表作为配置。这样在lua中直接require就可以使用配置了。转出来的配置表通常是这样的:
local t =
{
[] =
{
['id'] = ,
['icon'] = ,
['name'] = '默认头像框',
['description'] = '初始赠送',
['type_id'] = ,
['sort'] = ,
}, [] =
{
['id'] = ,
['icon'] = ,
['name'] = '10级头像框',
['description'] = '达到$1级即可获得',
['type_id'] = ,
['num'] = ,
['sort'] = ,
}, [] =
{
['id'] = ,
['icon'] = ,
['name'] = '20级头像框',
['description'] = '达到$1级即可获得',
['type_id'] = ,
['num'] = ,
['sort'] = ,
}, [] =
{
['id'] = ,
['icon'] = ,
['name'] = '30级头像框',
['description'] = '达到$1级即可获得',
['type_id'] = ,
['num'] = ,
['sort'] = ,
}, [] =
{
['id'] = ,
['icon'] = ,
['name'] = '40级头像框',
['description'] = '达到$1级即可获得',
['type_id'] = ,
['num'] = ,
['sort'] = ,
}, [] =
{
['id'] = ,
['icon'] = ,
['name'] = '50级头像框',
['description'] = '达到$1级即可获得',
['type_id'] = ,
['num'] = ,
['sort'] = ,
}, [] =
{
['id'] = ,
['icon'] = ,
['name'] = '60级头像框',
['description'] = '达到$1级即可获得',
['type_id'] = ,
['num'] = ,
['sort'] = ,
}, }
在某些功能中,是需要将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: ).
table: 0x003d8ff8
table: 0x003d92c8
table: 0x003d90e8
table: 0x003d8a58
table: 0x003da560
table: 0x003d91d8
table: 0x003d92f0
Program completed in 0.03 seconds (pid: ).
这样的代码,第一个取得的id并不一定是1。也就是说,lua虚拟机在编译这个文件时,里面的元素并不是按顺序的,即使key值是int型并且按从小到大并严格自增。
lua中的坑的更多相关文章
- Gink掉过的坑(一):将CCTableView导入到lua中
环境: 系统:win7 64位 cocos2dx:cocos2d-2.1rc0-x-2.1.3 Visual Studio: 2012 由于项目是用lua写的,需要将cocos2dx中的方法导入到lu ...
- Torch-RNN运行过程中的坑 [2](Lua的string sub函数,读取中文失败,乱码?)
0.踩坑背景 仍然是torch-rnn/LanguageModel.lua文件中的一些问题,仍然是这个狗血的LM:encode_string函数: function LM:encode_string( ...
- Torch-RNN运行过程中的坑 [1](读取Lua非空table,size为0)
0.踩坑背景 执行Torch-RNN的时候,在LanguageModel.lua中的encode_string函数中,对start_text的各个character进行id映射编码,实现功能类似“北京 ...
- 在redis一致性hash(shard)中使用lua脚本的坑
redis 2.8之前的版本,为了实现支持巨量数据缓存或者持久化,一般需要通过redis sharding模式来实现redis集群,普遍大家使用的是twitter开源的Twemproxy. twemp ...
- lua中遍历table的几种方式比较
当我在工作中使用lua进行开发时,发现在lua中有4种方式遍历一个table,当然,从本质上来说其实都一样,只是形式不同,这四种方式分别是: for key, value in pairs(tbtes ...
- Torch-RNN运行过程中的坑 [0](一些基础概念)
0.Lua & LuaJIT简介 Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. Lua 是巴 ...
- 从架构层面杜绝lua中使用未定义的变量
# 从架构层面杜绝lua中使用未定义的变量 标签(空格分隔): lua --- lua中有一个很坑的地方:1.就是如果一个变量拼写错误,会自动的认为你定义了一个值为nil的全局变量.2.如果在func ...
- lua中基类和“继承机制”
基类:基类定义了所有对于派生类来说普通的属性和方法,派生类从基类继承所需的属性和方法,且在派生类中增加新的属性和方法. 继承:继承是C++语言的一种重要机制,它允许在已定义的类的基础上产生新类. lu ...
- lua中实现异步资源读写
同样还是更新方面的需求,当我们检测到版本是新安装的以后,要进行upd目录清除.如果使用os.execute执行 rm -rf ooxx 是非常快的但由于os.execute一旦报错,那整个lua进程就 ...
随机推荐
- 【Android】无限滚动的HorizontalScrollView
这是一个很简单的功能,作为新手,做一下笔记.也给其它有需要的人提供一个参考. 首先HorizontalScrollView都知道用途了.它可以实现类似“桌面程序”左右切换页的效果.一般情况下里面的页数 ...
- linux下查看文件系统类型
1. df -hT命令 -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) -T, --pr ...
- 自定义Toast
简易自定义Toast public class MainActivity extends ListActivity );//边角 gradientDrawable.setGradien ...
- JavaScript基础(一)
JavaScript一.什么是JavaScript?脚本描述语言,网页交互特效,说白了,就是实现HTML实现不了的效果.(JavaScript是一种基于对象.事件驱动的简单脚本语言,嵌入在HTML文档 ...
- 腾讯云(centos7)上安装并配置PHP
1.查看yum上的php $ yum list php Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cach ...
- Android 聊天气泡
网上搜到的只有一篇是自定义的TextView,其使用比较麻烦,所以采用大众化的方法--使用9.png来实现. 这里主要介绍sdk tool的draw9patch.bat的使用. 这个bat执行文件打开 ...
- MeasureSpec学习
在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...
- Assembly 'Microsoft.Office.Interop.Excel
编译的时候报错,都无法通过编译: Assembly 'Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, Public ...
- DWZ简介及其使用
来源:http://blog.csdn.net/t123012009065/article/details/8286826 DWZ简介: DWZ富客户端框架(jQuery RIA framewor ...
- QT5-控件-QFontComboBox-字体选择下拉列表,使用一个标签查看效果
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QFontComboBox> ...