为什么不用 a= a or 1呢? 因为当传入的是boolean值时, false的话, false or 1也变1了.
这个是Lua的一种逻辑判断值的取值问题. 例如
a or b -- 当a = nil或false时, a or b这个逻辑判断表达式的值就是b, 当a不为false或者nil时, 那么这个表达式的值就是a.
> print(false or 1)
1
> print(0 or false)
0
> print(0 or "hello")
0
> print(nil or "hello")
hello
> print(false or "hello")
hello
a and b 则当a为真(不为false或nil)时, 表达式的结果等于b, 如果a为真, 则表达式的结果为a.
> print(1 and false)
false
> print(1 and nil)
nil
> print(1 and 2)
2
> print(false and "hello")
false
> print(nil and "hello")
nil
Lua支持一个函数返回多个值, 但是返回多个值时, 有多少个值传递, 或者被抛弃, 和用法有关, 比较诡异的地方来了.
> print(string.find("hello","llo"))
3 5
string.find("hello","llo")函数返回2个值, 第一个值为匹配到的字符串的开始位置, 第二个值为结束位置.
但是, 当多值函数后面再有表达式时, 多值函数就只返回第一个值, 后面的值被抛弃.
> print(string.find("hello","llo"), "hell") -- 这样的话string.find("hello","llo")只返回了第一个结果3. 5被抛弃了.
3 hell
在赋值时也一样
> a,b = string.find("hello","llo")
> print(a,b)
3 5
> a,b = string.find("hello","llo"), "he"
> print(a,b)
3 he
还有一种情况, 如果要强制使用仅仅返回1个结果的话, 在函数外加括号就可以. 这就是文章开头提到的括号用法.
> a=(string.find("hello","llo"))
> print(a)
3
> a=string.find("hello","llo")
> print(a)
3
> print(string.find("hello","llo"))
3 5
> print((string.find("hello","llo"))) -- 多值函数用括号包一层, 返回第一个结果值. 而不是整个list
3
多值函数例子 :
> function max(a)
local mi = 1 -- index
local m = a[mi]
for i=1,#a do
if a[i] > m then
mi = i; m = a[i]
end
end
return m, mi
end
> print(max({1,2,3,4,5}))
5 5
> print(max({1,2,3,4,5,100}))
100 6
比较容易记住Lua的多值返回函数的诡异的例子 :
> function foo0() end
> function foo1() return "a" end
> function foo2() return "a", "b" end
> x,y = foo2()
> print(x,y)
a b
> x = foo2()
> print(x)
a
> x,y,z = 10,foo2() -- 在这里, 也要注意, 后面有值的情况下, 多值函数都只取一个值. 没有值的话, 全取.
> print(x,y,z)
10 a b
> x,y = foo0()
> print(x,y)
nil nil
> x,y = foo1()
> print(x,y)
a nil
> x,y,z = foo2()
> print(x,y,z)
a b nil
> x,y = foo2(), "hello" -- 在这里, 也要注意, 后面有值的情况下, 多值函数都只取一个值.
> print(x,y)
a hello
> x,y = foo0(), "hello", "nihao"
> print(x,y)
nil hello
> t = {foo0(), foo2(), 4} -- 在这里, 也要注意, 后面有值的情况下, 多值函数都只取一个值.
> print(t)
table: 0x9515c0
> for i = 1,#t do
>> print(t[i])
>> i = i+1
>> end
nil
a
4
> t = {foo0(), foo2()}
> for i = 1,#t do
print(t[i])
i = i+1
end
nil
a
b
> print(foo2(),1) -- 在这里, 也要注意, 后面有值的情况下, 多值函数都只取一个值.
a 1
> print(foo2())
a b