lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Andy Stark wrote:
> >t = {"apple", "banana", "cherry",
> >"date"}
> >pick (t, 2, 4, 3, 4)  => { "banana", "date", "cherry", "date"}
> Ahem... yes, you're right - I forgot that the operation of 
> using a table as a key was already defined by default.
> There's nothing wrong in principle with using a function to 
> index a table in this way but it must be possible to pass 
> this function a table rather than just a variable length 
> argument list. A table can be generated at runtime whereas an 
> argument list is fixed in the source code.

Argument lists can be created at runtime with the unpack function:

t = {"apple", "banana", "cherry", "date"}
indices = {2, 4, 3, 4}
pick (t, unpack(indices))  => { "banana", "date", "cherry", "date"}

That way your pick function can ignore argument type and use tables as
indices transparently.