分类为‘lua’的日志

[Translate] 七天学习七门编程语言IOLanguage第一天

http://oscardelben.com/seven-languages-io-day-1

我刚买了Seven languages in seven weeks这本书的Beta版,真是一本好书。

继续阅读 »

Tags :

使用Lua编写Utility的一些小贴士

string trim功能

 1: function string_trim(s)

 2: if s == nil then

 3: return

 4: end

 5:  

 6: return (string.gsub(s, "^%s*(.-)%s*$", "%1"))

 7: end

检查某个字符串是不是指向合法目录。

 1: function check_directory(s)

 2: s = string_trim(s)

 3: if not s or s == "" then

 4: return false

 5: end

 6:  

 7: if string.sub(s, -1, -1) == [[\]]then

 8: s = string.sub(s, 1, -2)

 9: end

 10:  

 11: local attr = lfs.attributes (s)

 12: if (attr and attr.mode == "directory") then

 13: return true

 14: end

 15:  

 16: return false

 17: end

对于文件操作,最好先备份一下原来文件:

 1: local str = "copy \"" .. Folder .. '\\log.db' .. "\" \"" .. Folder .. '\\log.db' .. "." .. os.time() .. "\" /v /y"

 2:  

 3: os.execute(str)

对于sqlite的操作,注意使用了一个自增myid作为key。

 1: Db1 = sqlitelua.open(upFolder .. '\\mainlog.db')

 2:  

 3: Db1:exec('CREATE TABLE newfiles(myid INTEGER PRIMARY KEY AUTOINCREMENT, myname, myfolder)')

 4:  

 5: local stmt1 = Db1:prepare[[ INSERT INTO newfiles VALUES (:myid, :myname, :myfolded) ]]

 6:  

 7: stmt1:bind_names{myname = new_name, myfolder = currentFolder}

 8:  

 9: stmt1:step()

 10: stmt1:reset()

 11: stmt1:finalize()

Lua程序设计(第二版)阅读笔记

 

很有意思的是,下面的代码是有效地,我也是看了书才知道。代码后面可以跟着,也可以不跟分号。

 1: a = 5 b = 6

 2: c = 7; d = 8;

 3: print(a, b, c, d)

Lua中,什么是字母依赖于locale的设置,也就是中文环境下,中文可以用作变量名(?这个需验证)

Lua有大小写之分。

常用的块注释方式是–[[然后以--]]结尾,这样如果想取消块注释,就把开头多加个-就行了。

LUA_INIT内容为@文件名,解释器会先执行这个文件(?需验证)。

Lua中的(以后省略)函数为第一类值,比如print = type; print(a); 这个是合法合理的,但是print就没有了,一般用于沙箱sandbox操作。

条件判断只有两种情况为假,false以及nil。其余都为真。

number是实数(通常下)。用双精度代表整数,只要这个数字不大于10的14次方就没问题(本书这里笔误为1014,shit,我对照了PIL第一版确认的)。重新编译数字类型可以方便用于其它平台luaconf.h。数字可以写作4.57e-3 0.3e12 5e+20这种科学计数法。

Lua可以存储任意二进制字符到字符串类型中。5.1支持的长括号写法[===[匹配]===],只要等号数量移植。字符串与数字运算,数字会转成字符串,可以用tonumber把字符变成数字。

 1: local str = "5e+20"

 2: local num = tonumber(str)

 3: print(num)

数字转成字符,可以用tostring或者让数字与空字符串连接。

5.1的字符串支持#。

local a = {}创建了一个table,并且让a引用这个table,通过a这个变量我们可以操作这个table,如果没有变量继续引用这个table(a = nil),Lua会负责回收内存销毁它。

a["nameX“] = 5 等价于 a.nameX = 5,注意key里面的引号。local a = {}; a[x] = 5这个代码是不合法的。local a = {}; a["x"] = 5; print(a.x);这个会打印出5。

长度操作#的常用做法,print(a[#a])打印最后一个(因为Lua的table索引从1开始),a[#a +1] = n常用与自增,a[#a] = nil删除最后一个。

注意当table中有空洞(数字索引不连续,或者某个值为nil)的时候,#操作未必得到正确值。

5.1新增%取模操作。a % b == a – floor(a / b) * b,结果符号永远与第二个参数相同。print(5 % -2)得到-1,print(-5 % 2)得到1。另外取模操作可以用于对实数取整数部分或者小数部分。x = math.pi; x – x % 0.01 是让x精确到小数点后2位。x % 1 取小数部分,x – x % 1 取到整数部分。

table,userdata以及function,比较引用。

and与or都是用短路求值。 x = x or v常用于默认值设置。

Lua中的字符串是不可变值immutable value,两个字符串链接,会返会一个新的字符串。

链表写法:

 1: list = nil

 2: for line in io.lines() do

 3: list = {next = list, value = line}

 4: end

local a = { [-1] = 3, ["3"] = 5, ["n"] = "bbb"};

print(a[-1]); print(a["3"]); print(a.n);比较有意思的是a["3"]没法写成a.3。

a = {x = 0, y = 0} 相当于a = {["x"] = 0, ["y"] = 0}的语法特例。

local a = { [-1] = 3; ["3"] = 5; ["n"] = "bbb"};这样写也是合法的,构造式可以用分号代替逗号。

第四章

x, y = y, x交互x与y的值。

可以用do-end控制局部变量的范围。

local print = print,相当于定义了一个局部变量print(函数),后面如果使用print,就是访问了这个局部变量。访问局部变量要比访问全局变量快。

while x do .. end,当x为真值时进入while;repeat .. until x,当x为真值的时候结束循环。这点我弄混过。

Some interesting Lua code test

When I read the book <the ruby way>, I get some interesting code that need to run in Lua. (I will test following code in Lua5.14 for windows package.)

 1: print( 5 % 3)

 2: print( -5 % 3)

 3: print( 5 % -3)

 4: print(-5 % -3)

The result will be 2, 1, -1, -2.

 1: if not nil then

 2: print("not nil")

 3: end

 4:  

 5: if not false then

 6: print("not false")

 7: end

 8:  

 9: if 0 then

 10: print("0")

 11: end

 12:  

 13: if "" then

 14: print("empty string")

 15: end

The result will be "not nil", "not false", "0", "empty string", in Lua, only nil and false will be FALSE in condition checking.

 1: for i = 1, 5 do

 2: print(i)

 3: i = i + 5

 4: print(i)

 5: print("")

 6: end

OK, the result will be 1  6, 2  7, 3  8, 4  9, 5  10, you could see the index variable "i" is not changed for the for-loop, and you could assign new value (i=i+5) to it in the for range, but it will restore back to the loop index value again.

 1: local x, y = {}

 2: print(x)

 3: print(y)

 4:  

 5: y = x

 6: print(y)

 7: table.insert(y, "aa")

 8: print(x[1])

It will print like "table: 003CACB0" "nil" "table: 003CACB0" "aa".

Two thing need to note, local x, y = {} will only initialize the "x". Another is the y will have same address with x if x is a table, and insert to table y will cause x changes too. It seems like in c or c++ only copy the pointer address, not copy the value with the pointer.

Tags :

Howto install Lua5.14 in Debian

1) Install pre-require "sudo apt-get install build-essential readline-common libreadline-dev"

2) wget http://www.lua.org/ftp/lua-5.1.4.tar.gz

3) tar zvxf lua-5.1.4.tar.gz

4) cd lua-5.1.4

5) make linux

6) make linux install

7) test with "lua -v", should be 5.1.4.

That’s all!

Tags :

Lua 5.2 work1 浅谈

当前版本号为Lua 5.2.0 (work1),

1)增加了bitlib这个操作bit的内置库,函数有

{"band", b_and},
{"btest", b_test},
{"bor", b_or},
{"bxor", b_xor},
{"bnot", b_not},
{"bshift", b_shift},
{"brotate", b_rotate},

2)baselib中加入一个loadin,去掉了unpack。

3)几乎所有的ifndef xx都改成了if !defined(xx),所有的ifdef xx改成了if defined(xx)

4)增加了两个opcode,TFORCALL以及EXTRAARG。

5)增加了一个IN语句,类似

         IN exp DO block END

6)table库中加入pack以及unpack,去掉setn

7)其余的内部改动非常多,大部分源代码文件都有一些。

当然,这个版本是非常不报靠的,所以大家先睹为快,看看也就是了。

Tags : ,