I was trying to write a function with a variable number of arguments which do something for all its number entries. so I came up with something like this:
function luaFunc (...)
for i,v in ipairs{...} do
if type(v)=='number' then
--do something
end
end
end
but when i run this, it stops right on first non-number argument. whats the problem?
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
Try also this:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.