教程评论 |
|
作者注:如果您有重要的评论要发表,请提出一种纠正批评的方法。例如,如果您认为缺少主题或您想看到更多示例。请也发表积极的意见。如果教程的结构和风格受到欢迎,那么它可能会进一步扩展(作者会觉得他的努力是值得的!)。谢谢。
在下面添加评论
= expression,(2) true 和 false 未定义并变为 nil。起初,我不知道这一点让我感到害怕,后来我意识到使用 Lua 5.0 确实很有必要。如果教程在显眼的位置包含类似以下内容会怎么样
wget 实用程序制作维基的静态副本。
lua 并立即进行实验。我感谢简单、最小的维基页面布局。以下是反馈。--ShayGreen? --非常感谢您的宝贵意见 --NickCodeBlockTutorial? -- NickTroutbreak 关键字退出 while 语句的控制。break 关键字必须是块中的最后一个语句,即 end 关键字必须紧随其后。例如,... 我认为这样表达更清晰:...如果在块中使用 break,它必须是该块中 end 关键字之前的最后一个语句,因为 break 之后的任何内容都无法访问。break 之后添加额外的命令,因为这会导致编译错误。--NDTbreak 之后的任何内容*都*无法访问”。 'end' expected after 'break',而不是 'end' expected near 'some_symbol'),我们可以完全删除对它的提及。--PeterPrade
break(和 return)位于块的末尾。这会产生避免无法访问代码的副作用,但正如有人向我指出的那样,有时在调试期间禁用某些代码会很方便。你无法使用 break 来做到这一点,因为它会导致语法错误。 --RiciLake
file.lines 代表 file["lines"]。file:lines (...) 代表 file.lines (file, ...)。-- GavinWraith
在 TableLibraryTutorial 部分,以下部分在最新的 Lua 版本 (5.2) 中无法正常工作,据我所知,许多其他部分也存在同样的问题。您能提供并解释以下示例代码需要做哪些更改才能在最新版本中正常工作吗?
table.sort(table [, comp])
> t = { 3,2,5,1,4; n=3 } -- construct a table with user size of 3
> table.sort(t) -- sort will be limited by user size
> = table.concat(t, ", ") -- only specified size is concatenated as well
2, 3, 5
由于没有更好的替代方案,这里列出了从 Lua 5.1 教程中删除的旧 Lua 5.0 内容。
设置内存分配限制,当达到该限制时,垃圾收集器将被调用。如果新限制小于当前的 Lua 内存分配量,或者没有提供参数,则垃圾收集器将立即被调用。有关更多信息,请参阅 GarbageCollectionTutorial。
> = gcinfo() -- find current memory stats, i.e. 21kb used
21 35
> bigalloc = string.rep('a', 100000) -- create a big string
> = gcinfo() -- 164kb allocated now
164 327
> collectgarbage() -- force collection
> = gcinfo() -- we did free some memory on collection
139 278
> bigalloc = nil -- release the string we created
> = gcinfo() -- it's not deleted until its collected
140 278
> collectgarbage() -- collect
> = gcinfo() -- it's deleted
29 59
此函数将程序与动态 C 库 "libname" 链接。在该库中,它会查找函数 "funcname" 并将其作为 C 函数返回。libname 必须是 C 库的完整文件名,包括任何可能的路径和扩展名。此函数不受 ANSI C 支持。因此,它仅在某些平台上可用(Windows、Linux、Solaris、BSD 以及其他支持 dlfcn 标准的 Unix 系统)。
loadlib 使您能够使用自己编写的 C 函数来增强 Lua 脚本。以下示例将为您提供一些提示,如果您想实现自己的内容。此示例在 Linux 中经过测试,但在其他平台上也应该可以正常工作。您应该知道如何创建共享对象库。示例中的所有文件都应该位于同一个目录中。
/* mylib.c might look like this: */
#include "lua.h"
#include "stdio.h"
/* This function will be exported to lua */
static int lua_myfunc(lua_State* l)
{
printf("blabla");
return 0;
}
/* This function is our Initialization */
int init(lua_State* l)
{
printf("Registering personal functions");
lua_register(l, "myfunc", lua_myfunc);
printf("Done registering");
return 0;
}
现在将其编译为库(在 *nix 或 Cygwin 中)
gcc -Wall -g -O2 -shared `lua-config --include` -c mylib.c -o mylib.o gcc mylib.o -Wall -g -O2 -shared `lua-config --include` `lua-config --libs` -o mylib.a
之后,您应该会得到一个名为 mylib.a 的文件,这就是我们的库。现在让我们用 Lua 编写一个简单的测试脚本
luainit = loadlib("./mylib.a", "init")
-- now call the initialization routine
luainit()
print("New registered function: " .. myfunc)
-- start the new function
myfunc()
print("well done.")
> = math.mod(7,3) 1 > = math.mod(9,3) 0 > = math.mod(100,2.3) 1.1
手册简要介绍了该库的用途。我们在这里引用它
n" - 当表具有一个值为数字的字段 "n" 时,该值将被视为其大小。 setn - 可以使用 table.setn() 函数显式设置表格的大小。 nil 值的整数索引减 1。 table.getn() 和 table.setn() 函数的描述。注意: 表格的大小并不一定反映表格中包含的元素数量。这可能看起来有点奇怪,但它可能很有用,例如,用于维护非连续列表。
用于确定表格的大小。表格的大小在本页顶部进行了讨论。
> = table.getn({1,2,3}) -- Lua will count the elements if no size is specified
3
> = table.getn({1,2,3; n=10}) -- note, n overrides counting the elements
10
> t = {1,2,3}
> table.setn(t, 10) -- set our own size with setn()
> = table.getn(t)
10
> = table.getn({1,2,3,nil,5,6}) -- sequence ends at element 3 due to nil value at 4
3
设置表格的大小(请参阅有关表格大小和本页顶部的说明)。如果表格具有值“n”,则会更新它,例如:
> t = { 1,"two","three"; n=10 } -- create a table which has a user size specified
> = table.getn(t) -- read the size
10
> table.foreach(t, print) -- display the elements of the table
1 1
2 two
3 three
n 10
> table.setn(t, 12) -- use setn to change the size
> = table.getn(t) -- display the size
12
> table.foreach(t, print) -- display the table contents
1 1
2 two
3 three
n 12
如果表格没有键为 n 的元素,则表格的大小将在内部保存。
> t = { 1,"two",3,4 } -- no "n"
> = table.getn(t) -- calculate the size of the table
4
> table.setn(t, 10) -- set the size to a user defined value
> = table.getn(t) -- find the size
10
> table.foreach(t, print) -- look at the table contents
1 1
2 two
3 3
4 4