unpack, accepts a table as a parameter, and then returns all elements of the array according to the subscript
unpack
lua version <= 5.1
local t = {nil , 3}
retunrn unpack(t) // nil , 3
table.unpack
lua version > 5.1
local t = {nil, 3}
return table.unpack(t) // nil, 3
About the processing of indefinite number of parameters
Version 5.1 and earlier can be processed directly
local function fun1(...)
print(unpack(arg))
end
fun1(1,nil,3) //1,nil,3
After version 5.2, manual conversion is required
local function fun2(...)
local arg = { ... }
print(table.unpack(arg))
end
fun2(1,nil,3) //1,nil,3
table.length problem
There is a very interesting problem.
If the parameter is {nil, 2, nil}, it cannot be printed normally.
Why? After reading the documentation, I found that it is related to #table
table calculation method
That is to say, when there is a nil value, the calculation result of #table is not certain
local function fun(...)
local arg = {...}
print(table.unpack(arg))
for k, v in pairs(arg) do
print(k, v)
end
print(arg [1],arg[2],arg[3], #arg)
print('***************')
end
fun(nil, 2, nil)
fun(1,2,nil)
fun(1,nil,nil)
fun(nil,nil,3)
2 2
nil 2 nil 0
***************
1 2
1 1
2 2
1 2 nil 2
***************
1
1 1
1 nil nil 1
***************
nil nil 3
3 3
nil nil 3 3
***************
In conclusion, the result of #table is undefined when there are nil values in the table.
So when using table in the future, try not to set nil, but to table.remove