教程评论 |
|
作者注: 如果您有任何批评意见,请提出修正建议。例如,如果您认为某个主题缺失,或者您希望看到更多示例。也请多发表正面评价。如果您喜欢教程的结构和风格,那么它可以进一步扩展(作者也会觉得自己的努力是值得的!)。谢谢。
在此下方添加评论
= expression,(2)true 和 false 未定义,变成 nil。起初不知道,这让我感到害怕,然后我意识到确实有必要使用 Lua 5.0。教程是否可以在显眼的地方包含类似这样的内容?
wget 工具制作维基的静态副本。
lua 并立即进行实验。我欣赏简单、最小化的 Wiki 页面布局。反馈如下。--ShayGreen? --非常感谢您周到的评论 --NickCodeBlockTutorial? -- NickTroutbreak 关键字退出 while 语句的控制。break 关键字必须是块中的最后一个语句,即关键字 end 必须紧随其后。例如,... 我觉得这样更清楚:...如果 break 用于一个块中,它必须是该块中 end 关键字之前的最后一个语句,因为 break 之后的任何内容都不可达。break 之后的任何内容*都会*是不可达的”。'end' expected after 'break',而不是 'end' expected near 'some_symbol'),我们可以完全删除这条说明--PeterPrade
break(和 return)必须位于块的末尾才是正确的。这会避免代码不可达的副作用,但正如有人提醒我的,有时在调试时禁用一些代码会很好。你不能用 break 来做到这一点,因为这会是语法错误。 --RiciLake
file.lines 代表 file["lines"]。file:lines (...) 代表 file.lines (file, ...)。-- GavinWraith
ObjectOrientationTutorial 和 InheritanceTutorial 页面的质量似乎不如教程的其他部分。它们更像是代码示例而不是解释。--JohnBelmonte
在 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 的整数索引减一。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