Main Content

Lua - variable function arguments and select

Archive - Originally posted on "The Horse's Mouth" - 2010-09-08 07:56:47 - Graham Ellis

Lua's select function allows you to look at the variable arguments to your current function. If you're going to be calling a function from different places with different numbers of parameters, you can declare the function with its final parameter being "..." which sucks up the final parameters into an indexed table. However, #... to find out how long the extra table is contravenes the syntax rules of Lua, and that's where select comes in.

Looking at the various Lua books and sources, there's very little on select - it's a bit of an obscure corner - so I've added a full source example - [here] - on our web site.

Remember that you can also pass named parameters into a function in the form of a table - that's done by calling the function with curly braces rather than round brackets. That's included within a somewhat more complex example [here].