You can do this with variadic functions and the ... variable.
HOWEVER, in this case, I think one of the following alternatives would work better:
function weigh(values, weights)
for i = 1, #values do
local vi, wi = values[i], weights[i]
end
end
weigh( {1, 2, 3}, {5, 3, 7} )
function weight(data)
for i = 1, #data do
local vi, wi = data[i].value, data[i].weight
end
end
weigh{ {value=1, weight=5}, {data=2, weight=3}, {data=3, weight=7} }
If you know you aren't going to be repeating values, then a dictionary would be even better:
function weigh(data)
for vi, wi in pairs(data) do
end
end
weigh {
[1] = 5,
[2] = 3,
[3] = 7,
}
Variadic Functions and Tuples
Here is a good description of tuples.
Most of the time when you use , in Lua, that can be treated as a tuple. It's sort of like a list without a table holding it together.
If you use the special name ... as on of the parameters, then it's a tuple that holds all of the (remaining) parameters.
The easiest way to process it is to just dump it into a table (which is another reason why you normally just want to pass a list as the argument)
function weight(values, ...)
local weights = {...}
for i = 1, #values do
print(values[i], weights[i])
end
end
9 comments
does the table have the value?
Why would anyone answer. You'll just delete your question afterwards /:
I only delete my questions because I don't like getting alot of notifications
Smell that? It's the stench of a liar. Whatever I don't really care. I'm just not going to be helping you. Good luck.
Wow... sorry I deleted them okay?
You can manage your notifications in the setting btw. You can disable notifications from comments and such.
Ok, I will check it out
Sorry if I sounded mad. But you can understand when someone answers your question and then the question just gets deleted afterwards. It takes away the entire point of answering questions. But sorry. I hope we can remain... Not enemys.
Boy am I glad to hear that :)