lua-users home
lua-l archive

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


On Wed, Jun 10, 2009 at 2:02 PM, Duncan Cross<duncan.cross@gmail.com> wrote:
> On Wed, Jun 10, 2009 at 9:48 PM, Sam Roberts<vieuxtech@gmail.com> wrote:
> I'm glad someone else has already replied sensibly as I was in danger
> of making a bad joke about colour blindness, but I will add that if
> you want to iterate over the varargs of a function, when some of them
> may be nil, you have to use:

Yeah, I got that code pretty wrong! :)

> for i = 1,select('#', ...) do
>  local k = select(i, ...)
>  -- (stuff)
> end

Great, thanks! I've never used select, now I see the point.

> instead of:
>
> for i,k in ipairs{...} do
>  -- (stuff)
> end
>
> ...as ipairs will stop on the first nil.

Hm, you're right, strangely the table's length includes the nils...

> function f(...) local t={...} for i=1,#t do print(i,t[i]) end end
> f(1,nil,4)
1       1
2       nil
3       4


Thanks for all the suggestions, I like "packed = {n = select('#',
...), ...}", too.

Sam