作者yzugsr (Bird)
看板Programming
标题Re: [问题] Lua在函数中产生资料与读取问题
时间Sun Feb 10 00:54:55 2008
※ 引述《oohay (五黑)》之铭言:
: 其实我在做这件事情: 把一个外部变数x丢给某个函式,让函式帮x产生新内容.
: 不过以下方法全都产生foo, 所以我说看到鬼!
: 在写程式过程中,也做过很多假设,假设函式外的物件行为如何,假设函式内的物件
: 又是产生新实体或不产生新实体.
我试着厘清一下你的盲点
1.Lua的Table Literal会产生新的实体 (例如 a = {1,2} )
refer to
http://www.lua.org/pil/2.5.html
Java的Array也是
Ruby的Array, Python的List亦然
2.把Table当作Array使用 预设的index是从1开始
但是也可以从任何其他数值开始
refer to
http://www.lua.org/pil/11.1.html
: 至於说明书,当然不是没看罗! 但是看过了,写出来仍然与预期不符合!
: function a(y)
: table.insert(y, {1, 2})
: end
: function b(z)
: z = {1, 2}
: end
: x = {}
: if x[0] ~= nil then
: print(x[0][1] .. x[0][2])
: else
: print("foo")
: end
当然是foo
: a(x)
: if x[0] ~= nil then
: print(x[0][1] .. x[0][2])
: else
: print("foo")
: end
上述第二项 预设index从1开始
: x = {}
: b(x)
: if x[0] ~= nil then
: print(x[0][1] .. x[0][2])
: else
: print("foo")
: end
上述第一项
b(x)会产生新的实体
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.193.35.97