Lua functions can return multiple results :
a, b, c = unpack({'one', 'two', 'three'})
If I’m not interested in the third return value, I can choose to ignore it when calling the function :
a, b = unpack({'one', 'two', 'three'})
Is there a similar way to ignore the X first elements when calling the function ?
I could write this code if I only want the third return value, but I was wondering if a cleaner code exists :
_, _, c = unpack({'one', 'two', 'three'})
You can use the
selectfunction. It will return all arguments afterindex, whereindexis the first argument given toselect.Examples:
That said, I think in most cases, writing
_,_,c = f()is cleaner.selectis mostly useful when the argument number is not known in advance, or when chaining function calls together (e.g.f(select(2, g())))