lua-users home
lua-l archive

Re: table.pack()

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


> The table-argument-saving-in function that I'd like to see is the function 
> that returns a table that is the equivalent of several tables in its 
> argument concatenated.
Try this.
local function flatten(t,k,a)
	for i=1,#a do
		local v=a[i]
		if type(v)=="table" then
			k=flatten(t,k,a[i])
		else
			t[k]=v
			k=k+1
		end
	end
	return k
end
function table.flat(...)
	local t={}
	local n=select("#",...)
	local k=1
	for i=1,n do
		k=flatten(t,k,select(i,...))
	end
	return t,k
end
local t=table.flat(
{
{10},
{21,22,23},
{31,32,{331,332,333},34},
{40}
})
for i=1,#t do
	io.write(t[i] or i,' ')
end

AltStyle によって変換されたページ (->オリジナル) /