我试图编写一个具有可变数量参数的函数,该函数对其所有数字条目都做了一些事情。所以我想出了这样的事情:
function luaFunc (...)
for i,v in ipairs{...} do
if type(v)=='number' then
--do something
end
end
end
但是当我运行它时,它会在第一个非数字参数处停止。有什么问题?
local function luaFunc (...)
for i = 1, select('#',...) do
local v = select(i,...)
if type(v)=='number' then
--do something
print(v)
end
end
end
luaFunc (1,'a',nil,2) ]
-- Output
1
2
也试试这个:
function luaFunc (...)
local t=table.pack(...)
for i=1,t.n do
local v=t[i]
if type(v)=='number' then
print(i,v)
end
end
end
luaFunc(10,20,"hello",40,nil,60,print,99)