Lua学习整理

来源于百度文库

http://wenku.baidu.com/link?url=3zz_e54BtSn9lbwOX5MktSpv5OBCZXz1VNFb6b6D-FuPMVOn9WE7vjzstqFsbirYa4i0KsCrJK8w3Hv65w1IJSi6SzjyTx7iYO9IIGtsXf3

1,学习lua需要什么基础?

很显然,lua不适合作为你第一个编程语言,因为它需要比较深的c语言编程基础,而且对于数据结构有一定的了解,最关键的是它的功能函数并不完整,需要很多额外第三方支持,比如最基本的socket。所以学习lua最好有c或者c++的基础。

2,学习lua应该看什么文档?

最好从Programming In Lua (简称PIL)中文版看起,非常有帮助,最好是逐字逐句的看。文档在这里可以下载:http://groups.google.com/group/lua5 或者 http://sunxiunan.com/?p=1512

Lua for windows这个整合安装包中包含了PILLua manual这两个文档的英文版。

3,如何在windows下使用lua

可以在Luaforge上找到编译好的Lua lib/dll/exe

windows下最好是使用Lua for windows http://luaforge.net/projects/luaforwindows/,里面会包含一些常用的模块,比如luasocketluasqlsqlite等。而且还包含了一个编辑器Scite,可以通过它来对lua脚本进行简单的调试,不需要额外安装什么IDE了。

如果你要自己编译Lua,可以在lua.org下载Lua的源代码,解压以后,参考readme文件,里面介绍的很详细。如果使用VC2008,也可以直接下载lua_vc2008.rar

4lua如何进行网络编程?

通过luasocket这个扩展库。具体信息可以在http://luasocket.luaforge.net/ 找到,另外Lua for windows附带了luasocket,安装后可以直接使用。、

5.1lua可不可以与c交互?

PIL以及Lua manual上介绍了如何使用c语言编写lua的第三方扩展,另外可以参考我的文章与示例代码http://sunxiunan.com/?p=1498

c语言中调用lua也非常方便,可以静态调用(通过lib),也可以动态调用(通过dll),使用lua增强c语言这样静态编译语言的能力是非常有趣的。

5.2lua可不可以与c++交互?

c++可以通过c语言的方式与lua交互。另外c++可以通过luabind或者luaplus这样的第三方库支暴露类与类成员信息给lua使用,可以方便(?)c++开发者。

我个人对于c++lua交互的看法可以参考勿用屠龙来杀猪-论如何正确整合LuaC++

5.3lua可不可以与dotnet交互?

可以通过luainterface这个项目在lua中使用dotnet platform,或者在c# 中使用Lua,但是这个项目的稳定度与成熟度都还达不到可用的标准。

5.4lua可不可以直接调用windows api

通过lua alien这个扩展库

6lua除了编写魔兽世界的插件,还能做什么?

可以用lua来写一些常用的程序,比如操作excel,比如定制一些查找。还可以把lua作为一种配置方式(类似ini文件或者cfg文件),因为 lua的表,可以达到非常复杂的配置功能,另外lua的解析速度要比xml快多了。当然lua主要用处还是作为游戏的脚本支持语言。

7lua可以调用windows api么?可以调用COM组件么?

可以自己编写一个dll封装对windows api的调用,或者用alien这个模块。可以通过luacom来调用COM组件。BTW,通过COM组件调用可以实现对excel或者word的操作。

云风介绍了一种windows api调用的方法:http://blog.codingnow.com/cloud/LuaApiCall

8,学习、使用lua有什么必须知道的网站么?

http://lua-users.org

http://lua.org

http://luaforge.org

http://lua-faq.org

http://www.keplerproject.org/

另外在云风http://blog.codingnow.com/的博客上也有不少关于Lua的好文章。

9C/C++调用lua(比如使用dofile)常见的问题?

C++调用lua,必须用绝对路径(c:\aaa\bbb.lua)而不是相对路径。否则Lua虚拟机找不到这个文件。

10Lua可以编译后执行么?

可以通过luaclua代码编译成Lua的虚拟机指令集。这样可以隐藏原来代码,在一些内存或者CPU受限的环境下直接使用编译后的虚拟机也可节省硬件资源。

11Lua能面向对象么?Lua为什么没有类?Lua为什么没有多态?Lua为什么没有C++或者Java或者C#的这种那种特性?

Lua是一种单独的语言!在语言的特性以及实现上,Lua设计者有他们的取舍。高效、简洁、方便是Lua的特点。Lua的面向对象特性类似 Javascript,都是基于原型机制(而非类机制)实现。类不是面向对象的必要特性。Lua的变量没有类型定义(值value有),所以也不存在多态 这种机制,就如同c++不容易实现duck typing一样,取舍取舍,取了这样自然要舍弃那样。

Lua中实现OO特性,可以参考下面的链接:http://lua-users.org/wiki/ObjectOrientedProgramming

或者在luaforge上搜索object,可以找到很多相关项目,如LOOP

另外在PIL中也介绍了如何实现OO的方法。也可以参考云风的实现方式:http://blog.codingnow.com/cloud/LuaOO

12Lua是否支持Unicode字符串?

Lua字符串可以容纳任何字符(包括0),所以如果想把unicode字符存到Lua字符串中没有任何问题,但是Luastring标准库只能处理单字节,所以想编程使用Unicode,需要另外支持,如icu4lua,详细信息参考:

http://lua-users.org/wiki/LuaUnicode

13a.f(x)a:f(x)有什么区别?

简单的说,a:f(x)a.f(a, x)的一种简写形式,更方便函数调用。

14requiredofile有什么区别?

这两个函数都会载入并且运行lua脚本,区别是,require只需要指定模块名字(不需要加入.lua .dll这样后缀),而dofile需要指定文件的完整的实际路径。

windows下输入以下命令lua -e "print(package.path)"来显示package查找路径。

第二个区别是require会记住load过的信息,重复调用不会导致模块被重新载入(reload)。(我们可以通过dofile这种特性来实现热更新)

第三个区别是require可以载入二进制模块,如c语言编写的动态库,可以通过package.cpath来显示动态库查找的路径信息。

如果你想载入的动态库是在某个子目录下,如c:\lua\5.1\lib\luasockets\core,可以通过点号形式来require,如require socket.core

15,如何显式载入一个二进制动态库(dll)?

可以通过package.loadlib来实现。

16Lua有什么优化技巧么?

可以通过LuaProfiler来查看你程序中的瓶颈http://luaprofiler.luaforge.net,另外Lua gems中有篇文章非常值得参考Lua Performance Tips

也可以到http://lua-users.org/wiki/OptimisationCodingTips 来查看他人的经验。

17Luaexception机制么?

没有内置的,但是可以通过pcall来间接实现。
local status,err = pcall(function()
t.alpha = 2.0 — will throw an error if t is nil or not a table
end)

if not status then
print(err)
end

18LuaC交互时,能否管理用户对象的生存期?

The Implementation of Lua5.0中有介绍,userdata可以认为是用户指定的一块内存指针,分两种:Heavy userdata是由Lua来做内存分配并且管理GC的,而Light userdata完全由用户掌控内存分配销毁。

也可以参考云风的一篇文章:http://blog.codingnow.com/2007/10/lua_c_object_reference.html

19,优化的尾调用optimized tail calls可以做什么?

状态机实现:
– Define State A
function A()
dosomethingin state A
if somecondition() then return B() end
if done() then return end
return A()
end

– Define State B
function B()
dosomethingin state B
if othercondition() then return A() end
return B()
end

– Run the FSM, starting in State A
A()

20,我想把lua打包成exe,如何做?

可以使用srluaL-Bia这样的lua项目,另外可以使用lua2c这个工具把lua代码编译成c语言代码,然后通过includec语言中直接使用。

21,什么是函数环境Function Environments

简单的说,函数环境就是全局表global table。可以用来实现sandbox沙箱功能。
function test ()
return A + 2*B
end
t = { A = 10; B = 20 }
setfenv(test,t)
print(test())
=>
50

22,如何实现命名参数named parameters
function named(t)
local name = t.name or anonymous
local os = t.os or <unknown>
local email = t.email or t.name..@..t.os

end
named {name = bill, os = windows}

23Lua有没有Ruby gem这种机制?

如果想自动安装更新第三方模块,可以使用luarocks

24Lua支持字符串正则查找替换么?

支持,参考string标准库。也可以使用lpeg这个第三方模块。

25,如何在c语言中遍历一个Lua table
/* table is in the stack at index t’ */
lua_pushnil(L); /* first key */
while (lua_next(L, t) != 0) {
/* uses key’ (at index -2) and value’ (at index -1) */
printf(%s – %s\n,
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));

/* removes value; keeps key’ for next iteration */
lua_pop(L, 1);
}

26 使用Lua stack需要注意的地方?
在 函数中,不能随意的时候 lua_State 中的虚拟机堆栈,如果需要大量使用堆栈,应该先调用 lua_checkstack 。少量使用堆栈,(在 LUA_MINSTACK 20 )之下时则没有问题。(from云风blog

27 当从 lua 调用 函数时,当参数数量不足的时候,并不会填入 nil 作为缺省参数。比如,写了一个 函数,接受两个参数。当 lua 中调用这个 函数时,如果仅传入一个参数,那么在 中 stack 上 index 2 位置的值并不一定是 nil 。这个时候我们应该用 lua_gettop 得到准确的参数个数以做适当的处理,或者直接在进入 函数时调用一次 lua_settop(L,2) 强制堆栈扩展到两个。

28 在生成 cclosure 的时候,upvalue 不能超过 255 个。而这一点并没在文档中说明,运行时压入超过 255 个 upvalue 也不会报错。

以上3条来自http://blog.codingnow.com/2006/09/lua_cclosure_upvalue.html

29 判断一个字符串的前缀是不是 @
有三个方案:

1. 比较直观的是 string.sub(str,1,1) == @
2. 感觉效率比较高的是 string.byte(str) == 64
3. 或者是 string.find(str,@) == 1

30 http://www.lua.org/demo.html 这个演示功能怎么实现的?

http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/,查找demo能看到它的源代码。

31 mav · An experimental tool for memory allocation visualization. Need to add

32 树形打印table,类似phpprint_r

from http://blog.codingnow.com/cloud/LuaPrintR
local print = print
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local type = type
local pairs = pairs
local tostring = tostring
local next = next
function print_r(root)
local cache = { [root] = .” }
local function _dump(t,space,name)
local temp = {}
for k,v in pairs(t) do
local key = tostring(k)
if cache[v] then
tinsert(temp,+” .. key .. ” {” .. cache[v]..})
elseif type(v) == table” then
local new_key = name .. .” .. key
cache[v] = new_key
tinsert(temp,+” .. key .. _dump(v,space .. (next(t,k) and |” or ” ” ).. srep(” “,#key),new_key))
else
tinsert(temp,+” .. key .. ” [" .. tostring(v).."])
end
end
return tconcat(temp,\n..space)
end
print(_dump(root, “”,"))
end

33 如何可以安全的迭代一个table

http://blog.codingnow.com/2009/03/safe_set.html

34 lua编译时计算实现

http://blog.codingnow.com/2008/08/compile_time_calculation_in_lua.html

35 如何通过closure模拟table
from http://blog.codingnow.com/2006/03/closure_table.html
function point (x,y)
return function () return x,y end
end
可以用 point(1,2) 构造一个 point 。它比 {1,2} 轻量。
还可以有一个更复杂一点的实现:
function point (x,y)
return function (idx)
if idx==x” then return x
elseif idx==y” then return y
else return x,y end
end
end

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
来源: http://lua-users.org/wiki/LuaUnicode 目录: | LuaUnicode.url | +---0.13A | ICU4Lua-0.13A-src.zip | ICU4Lua-0.13A-win32-dll.zip | \---0.2B ICU4Lua-0.2B-docs.zip ICU4Lua-0.2B-src.zip ICU4Lua-0.2B-win32dll.zip 下面的来源于: http://lua-users.org/wiki/LuaUnicode This is an attempt to answer the LuaFaq : Can I use unicode strings? or Does Lua support unicode? In short, yes and no. Lua gives you the bare bones support and enough rope and not much else. Unicode is a large and complex standard and questions like "does lua support unicode" are extremely vague. Some of the issues are: Can I store and retrieve Unicode strings? Can my Lua programs be written in Unicode? Can I compare Unicode strings for equality? Sorting strings. Pattern matching. Can I determine the length of a Unicode string? Support for bracket matching, bidirectional printing, arbitrary composition of characters, and other issues that arise in high quality typesetting. Lua strings are fully 8-bit clean, so simple uses are supported (like storing and retrieving), but there's no built in support for more sophisticated uses. For a fuller story, see below. Unicode strings and Lua strings A Lua string is an aribitrary sequence of values which have at least 8 bits (octets); they map directly into the char type of the C compiler. (This may be wider than eight bits, but eight bits are guaranteed.) Lua does not reserve any value, including NUL. That means that you can store a UTF-8 string in Lua without problems. Note that UTF-8 is just one option for storing Unicode strings. There are many other encoding schemes, including UTF-16 and UTF-32 and their various big-endian/little-endian variants. However, all of these are simply sequences of octets and can be stored in a Lua string without problems. Input and output of strings in Lua (using the io library) uses C's stdio library. ANSI C does not require the stdio library to handle arbitrary octet sequences unless the file is opened in binary mode; furthermore, in non-binary mode, some octet sequences are converted into other ones (in order to deal with varying end-of-line markers on different platforms). This may affect your ability to do non-binary file input and output of Unicode strings in formats other than UTF-8. UTF-8 strings will probably be safe because UTF-8 does not use control characters such as \n and \r as part of multi-octet encodings. However, there are no guarantees; if you need to be certain, you must use binary mode input and output. (If you do so, line-endings will not be converted.) Unix file IO has been 8-bit clean for a long while. If you are not concerned with portability and are only using Unix and Unix-like operating systems, you can almost certainly not worry about the above. If your use of Unicode is restricted to passing the strings to external libraries which support Unicode, you should be OK. For example, you should be able to extract a Unicode string from a database and pass it to a Unicode-aware graphics library. But see the sections below on pattern matching and string equality. Unicode Lua programs Literal Unicode strings can appear in your lua programs. Either a UTF-8 encoded string can appear directly with 8-bit characters or you can use the \ddd syntax (note that ddd is a decimal number, unlike some other languages). However, there is no facility for encoding multi-octet sequences (such as \U+20B4); you would need to either manually encode them to UTF-8, or insert individual octets in the correct big-endian/little-endian order (for UTF-16 or UTF-32). Unless you are using an operating system in which a char is more than eight bits wide, you will not be able to use arbitrary Unicode characters in Lua identifers (for the names of variables and so on). You may be able to use eight-bit characters outside of the ANSI range. Lua uses the C functions isalpha and isalnum to identify valid characters in identifiers, so it will depend on the current locale. To be honest, using characters outside of the ANSI range in Lua identifiers is not a good idea, since your programs will not compile in the standard C locale. Comparison and Sorting Lua string comparison (using the == operator) is done byte-by-byte. That means that == can only be used to compare Unicode strings for equality if the strings have been normalized in one of the four Unicode normalizations. (See the [Unicode FAQ on normalization] for details.) The standard Lua library does not provide any facility for normalizing Unicode strings. Consequently, non-normalized Unicode strings cannot be reliably used as table keys. If you want to use the Unicode notion of string equality, or use Unicode strings as table keys, and you cannot guarantee that your strings are normalized, then you'll have to write or find a normalization function and use that; this is non-trivial exercise! The Lua comparison operators on strings (< and <=) use the C function strcoll which is locale dependent. This means that two strings can compare in different ways according to what the current locale is. For example, strings will compare differently when using Spanish Traditional sorting to that when using Welsh sorting. It may be that your operating system has a locale that implements the sorting algorithm that you want, in which case you can just use that, otherwise you will have to write a function to sort Unicode strings. This is an even more non-trivial exercise. UTF-8 was designed so that a naive octet-by-octet string comparison of an octet sequence would produce the same result if a naive octet-by-octet string comparison were done on the UTF-8 encoding of the octet sequence. This is also true of UTF-32BE but I do not know of any system which uses that encoding. Unfortunately, naive octet-by-octet comparison is not the collation order used by any language. (Note: sometimes people use the terms UCS-2 and UCS-4 for "two-byte" and four-byte encodings. These are not Unicode standards; they come from the closely corresponding ISO standard ISO/IEC 10646-1:2000 and currently differ in that they allow codes outside of the Unicode range, which runs from 0x0 to 0x10FFFF.) Pattern Matching Lua's pattern matching facilities work character by character. In general, this will not work for Unicode pattern matching, although some things will work as you want. For example, "%u" will not match all Unicode upper case letters. You can match individual Unicode characters in a normalized Unicode string, but you might want to worry about combining character sequences. If there are no following combining characters, "a" will match only the letter a in a UTF-8 string. In UTF-16LE you could match "a%z". (Remember that you cannot use \0 in a Lua pattern.) Length and string indexing If you want to know the length of a Unicode string there are different answers you might want according to the circumstances. If you just want to know how many bytes the string occupies, so that you can make space for copying it into a buffer for example, then the existing Lua function string.len will work. You might want to know how many Unicode characters are in a string. Depending on the encoding used, a single Unicode character may occupy up to four bytes. Only UTF-32LE and UTF-32BE are constant length encodings (four bytes per character); UTF-32 is mostly a constant length encoding but the first element in a UTF-32 sequence should be a "Byte Order Mark", which does not count as a character. (UTF-32 and variants are part of Unicode with the latest version, Unicode 4.0.) Some implementations of UTF-16 assume that all characters are two bytes long, but this has not been true since Unicode version 3.0. Happily UTF-8 is designed so that it is relatively easy to count the number of unicode symbols in a string: simply count the number of octets that are in the ranges 0x00 to 0x7f (inclusive) or 0xC2 to 0xF4 (inclusive). (In decimal, 0-127 and 194-244.) These are the codes which can start a UTF-8 character code. Octets 0xC0, 0xC1 and 0xF5 to 0xFF (192, 193 and 245-255) cannot appear in a conforming UTF-8 sequence; octets in the range 0x80 to 0xBF (128-191) can only appear in the second and subsequent octets of a multi-octet encoding. Remember that you cannot use \0 in a Lua pattern. For example, you could use the following code snippet to count UTF-8 characters in a string you knew to be conforming (it will incorrectly count some invalid characters): local _, count = string.gsub(unicode_string, "[^\128-\193]", "") If you want to know how many printing columns a Unicode string will occupy when you print it out using a fixed-width font (imagine you are writing something like the Unix ls program that formats its output into several columns), then that is a different answer again. That's because some Unicode characters do not have a printing width, while others are double-width characters. Combining characters are used to add accents to other letters, and generally they do not take up any extra space when printed. So that's at least 3 different notions of length that you might want at different times. Lua provides one of them (string.len) the others you'll need to write functions for. There's a similar issue with indexing the characters of a string by position. string.sub(s, -3) will return the last 3 bytes of the string which is not necessarily the same as the last three characters of the string, and may or may not be a complete code. You could use the following code snippet to iterate over UTF-8 sequences (this will simply skip over most invalid codes): for uchar in string.gfind(ustring, "([%z\1-\127\194-\244][\128-\191]*)") do -- something end More sophisticated issues As you might have guessed by now, Lua provides no support for things like bidirectional printing or the proper formatting of Thai accents. Normally such things will be taken care of by a graphics or typography library. It would of course be possible to interface to such a library that did these things if you had access to one. There is a little string-like package [slnunicode] with upper/lower, len/sub and pattern matching for UTF-8. See ValidateUnicodeString for a smaller library. [ICU4Lua] is a Lua binding to ICU (International Components for Unicode [1]), an open-source library originally developed by IBM. See UnicodeIdentifers for platform independent Unicode Lua programs.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值