lua-users home
lua-l archive

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


> > function foo ()
> >   return {6, "hello", {}, 32}
> > end
> > 
> > Result = foo()
> > NumValues = getn(Result)
> > FirstValue = Result[1]
> > LastValue = Result[NumValues]
>
> Returning a table is fine, but if you insist in returning a list of values
> instead of a table, use "unpack", introduced in 4.1 (alpha).
> --lhf

You may want to add this to your program to make sure that it will
also run on older Luas.

unpack = unpack or function(arr)	-- note: arr will lose its elements!
  if getn(arr)==0 then return end
  if getn(arr)==1 then return arr[1] end
  local firstelement = tremove(arr, 1)
  return firstelement, unpack(arr)
end

Eduardo Ochs
http://angg.twu.net/
http://www.mat.puc-rio.br/~edrx/
edrx@inx.com.br