Reputation: 93
For lua, according to this tutorial, triple dots of a function can be accessed by a hidden variable named arg.
https://www.lua.org/pil/5.2.html
I write a very simple program
require 'torch'
function triDot(...)
print('in triDot now')
print(arg)
for i,v in ipairs(arg) do
print('i is',i,'v is',v)
end
end
triDot('name1','name2')
It turns out that arg doesn't hold {'name1', 'name2'} at all but a bunch of system parameters. For loop yields nothing.
{ 0 : "/home/jun/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th" -2 : "-e" -5 : "/home/jun/torch/install/bin/luajit" -3 : "package.path="/home/jun/.luarocks/share/lua/5.1/?.lua;/home/jun/.luarocks/share/lua/5.1/?/init.lua;/home/jun/torch/install/share/lua/5.1/?.lua;/home/jun/torch/install/share/lua/5.1/?/init.lua;"..package.path; package.cpath="/home/jun/.luarocks/lib/lua/5.1/?.so;/home/jun/torch/install/lib/lua/5.1/?.so;"..package.cpath" -4 : "-e" -1 : "local k,l,_=pcall(require,"luarocks.loader") _=k and l.add_context("trepl","scm-1")" }
Can anyone help in this?
Upvotes: 7
Views: 7474
Reputation: 26744
arg
doesn't work for function parameters in Lua 5.1+ (it could work in 5.1 with some compatibility options turned on; it only works for script parameters). You need to use local arg = {...}
to assign function parameters to a table or use select(i, ...)
to get i-th parameter from the list and select('#', ...)
to get the number of parameters. The former is simpler, but the latter option can deal with nil
values in the passed parameters, so use it if you expect nil
values.
Upvotes: 18