我在我正在使用的项目中遇到以下代码。我不明白for循环的迭代部分。什么是select()函数?
function _log (str,...)
local LOG="/tmp/log.web"
for i=1,select('#',...) do
str= str.."\t"..tostring( select(i,...) )
end
os.execute("echo \"".. str .."\" \>\> " .. LOG )
end
答案 0 :(得分:9)
来自Lua手册:
If index is a number, returns all arguments after argument number
index. Otherwise, index must be the string "#", and select returns
the total number of extra arguments it received.
Lua has built in multiple arguments,您可以将其转换为 表如果你真的需要:
function multiple_args(...)
local arguments = {...} -- pack the arguments in a table
-- do something --
return unpack(arguments) -- return multiple arguments from a table (unpack)
end
最后,如果将“#”作为索引传递,则该函数返回所提供的多个参数的计数:
print(select("#")) --> 0
print(select("#", {1, 2, 3})) --> 1 (single table as argument)
print(select("#", 1, 2, 3)) --> 3
print(select("#", {1,2,3}, 4, 5, {6,7,8}) --> 4 (a table, 2 numbers, another table)
请参阅此website。