lua-users home
lua-l archive

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



On Sep 14, 2005, at 02:12, Luiz Henrique de Figueiredo wrote:

The use of "arg" inside vargars function is only supported for compatibility.
If you need a table with all args, use {...}. No need for pack.

Hmmm... but... as mentioned by Michael.. would that handle arguments with nil values as well?


function test1( ... )
    for anIndex, aValue in ipairs( { ... } ) do
	    print( "test1", anIndex, aValue )
    end
end

test1( "first", "second", nil, "third" )

> test1   1       first
> test1   2       second

function test2( ... )
        for anIndex = 1, select( "#", ... ) do
                local aValue = select( anIndex, ... )

                print( "test2", anIndex, aValue )
        end
end

> test2   1       first
> test2   2       second
> test2   3       nil
> test2   4       third

function test3( ... )
        local someArguments = { ... }
        local aSize = select( "#", ... )

        for anIndex = 1, aSize do
                local aValue = someArguments[ anIndex ]

                print( "test3", anIndex, aValue )
        end
end

> test3   1       first
> test3   2       second
> test3   3       nil
> test3   4       third

What's the proper way to enumerate vararg then?

Thanks.

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/