lua-users home
lua-l archive

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


Here is my contribution for doing it recursively :-)

function inner_append(n, x, arg1, ...)
	if n <= 0 then
		return x
	end
	return arg1, inner_append(n - 1, x, ...)
end

function append(x, ...)
	return inner_append(select("#", ...), x, ...)
end


local t = {append(10, 1, 2, 3)}
assert(t[1] == 1)
assert(t[2] == 2)
assert(t[3] == 3)
assert(t[4] == 10)
assert(t[5] == nil)

local t = {append(10)}
assert(t[1] == 10)
assert(t[2] == nil)


On Tue, Nov 11, 2008 at 1:34 PM, Jun Wu <idearet@gmail.com> wrote:
> Thanks all the people. I realized the problem.
> "," (and many other operators, such as "()", "[]" etc.) will truncate the
> variables before it.
>
> Now I also believe a table must be created to append a value to "...".
> To append a value to "..." more efficiently, low level stack operations are
> needed. As Peter said, a C function is a better choice.
>
>
> On Tue, Nov 11, 2008 at 8:06 PM, Duncan Cross <duncan.cross@gmail.com>
> wrote:
>>
>> On Tue, Nov 11, 2008 at 11:39 AM, Jun Wu <idearet@gmail.com> wrote:
>>>
>>> Sorry. But I have mistyped  ...
>>> unpack{ ... , "a" } just works :p
>>
>> It doesn't. It will truncate ... to its first value (or expand it to nil
>> if there were no varargs) just the same as if you tried to directly return
>> ..., "a"
>
>