lua-users home
lua-l archive

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



Am 26.06.2013 17:06 schrieb "Daniel Barna" <daniel.barna@cern.ch>:
>
> Hi, I have a function which return a list of variables:
> function some_function()
>   return a,b,c,d;
> end
>
> I would like to write this to a file, using a whitespace as the separator, something like this:
> some_file:write(some_function());
>
> How can I do this?
> Thank you
> Daniel
>

How about this:

function concat(sep,...)

local t = {}

for i=1,select('#',...) do

t[i] = tostring(select(i,...))

end

return table.concat(t,sep)

end


function test() return 1,nil,2,nil,nil end

print(concat(", ",test()))

---

output:
1, nil, 2, nil, nil

As pointed out before by Thijs Schreijer, wrapping arguments into tables causes trouble when nil values are around. Besides, nonstring values can also be problematic with concat. I am not sure, but the code I wrote should handle both cases correctly.

Cheers,
Eike