http://www.hackhome.com/InfoView/Article_80376_7.html
使用 Lua 编写可嵌入式脚本
日期:2007年6月2日 作者: 查看:[ 大字体 中字体 小字体]-
-
或者像在其他解释性语言中一样,我们可以在代码顶部添加一行 “标识符”(#!),使这个脚本变成可执行的,然后像单独命令一样来运行这个文件:
$ (echo '#! /usr/bin/lua'; cat factorial.lua) > factorial $ chmod u+x factorial $ ./factorial enter a number: 4 24
Lua 语言
Lua 具有现代脚本语言中的很多便利:作用域,控制结构,迭代器,以及一组用来处理字符串、产生及收集数据和执行数学计算操作的标准库。在 Lua 5.0 Reference Manual 中有对 Lua 语言的完整介绍(请参见 参考资料)。
在 Lua 中,只有值 具有类型,而变量的类型是动态决定的。Lua 中的基本类型(值)有 8 种: nil,布尔型,数字,字符串,函数,线程,表 以及 用户数据。前 6 种类型基本上是自描述的(例外情况请参见上面的 Lua 特性 一节);最后两个需要一点解释。
Lua 表
在 Lua 中,表是用来保存所有数据的结构。实际上,表是 Lua 中惟一的 数据结构。我们可以将表作为数组、字典(也称为散列 或联合数组)、树、记录,等等。
与其他编程语言不同,Lua 表的概念不需要是异构的:表可以包含任何类型的组合,也可以包含类数组元素和类字典元素的混合体。另外,任何 Lua 值 —— 包括函数或其他表 —— 都可以用作字典元素的键值。
要对表进行浏览,请启动 Lua 解释器,并输入清单 1 中的黑体显示的代码。
清单 1. 体验 Lua 表$ lua > -- create an empty table and add some elements > t1 = {} > t1[1] = "moustache" > t1[2] = 3 > t1["brothers"] = true > -- more commonly,create the table and define elements > all at once > t2 = {[1] = "groucho",[3] = "chico",[5] = "harpo"} > t3 = {[t1[1]] = t2[1],accent = t2[3],horn = t2[5]} > t4 = {} > t4[t3] = "the marx brothers" > t5 = {characters = t2,marks = t3} > t6 = {["a night at the opera"] = "classic"} > -- make a reference and a string > i = t3 > s = "a night at the opera" > -- indices can be any Lua value > print(t1[1],t4[t3],t6[s]) moustache the marx brothers classic > -- the phrase table.string is the same as table["string"] > print(t3.horn,t3["horn"]) harpo harpo > -- indices can also be "multi-dimensional" > print (t5["marks"]["horn"],t5.marks.horn) harpo harpo > -- i points to the same table as t3 > = t4[i] the marx brothers > -- non-existent indices return nil values > print(t1[2],t2[2],t5.films) nil nil nil > -- even a function can be a key > t = {} > function t.add(i,j) >> return(i+j) >> end > print(t.add(1,2)) 3 > print(t['add'](1,2)) 3 > -- and another variation of a function as a key > t = {} > function v(x) >> print(x) >> end > t[v] = "The Big Store" > for key,value in t do key(value) end The Big Store
- 上一页 [1] [2] [3] [4] [5] [6] [7] 下一页
============================================
值得关注的Lua语言 收藏
新一篇: C-Runtime library的string trim函数(学无止境:C函数参考手册需要补课,汗!) | 旧一篇: 线程并发,用标志位同步时要小心
值得关注的Lua语言札记:
1.Lua流行的速度很快,缘于WOW的缘故吧。
2.参考了一下WOW的脚本,感觉多语言、pet动作的定义可以由lua来完成,这可能是一个好的idea,先看看是否能够通过移植这一关吧
3.这个学习lua的系列不错,写得比较深入,如果有心人看到的话,推荐一下: http://blog.csdn.net/kun1234567/
4.从 http://www.WOWF1.com (大脚, Big foot)这个网址,可以下载WOW的插件,解压后,其中有一些.lua文件,这是最好的lua的例子
注:
前面随便贴了篇DW上的文章留意要关注的小编,被网友看到,以致被误会忽悠,甚歉!(俺可不是本山 :-))
发表于 @ 2007年11月01日 18:48:00|评论(2)|编辑
新一篇: C-Runtime library的string trim函数(学无止境:C函数参考手册需要补课,汗!) | 旧一篇: 线程并发,用标志位同步时要小心
http://blog.csdn.net/hylaking/archive/2007/11/01/1861745.aspx
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。