Return a variable number of values from function

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

Return a variable number of values from function

Matthias Gall
Hi all,

I just asked myself whether it's possible to have something like the vararg
function for function results: a function returns a variable number of
values. I would be happy about a short code snippet, if this feature is
supported. I am using LUA 4.0.

Greetings,
Matt


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Steve Dekorte-4

On Sunday, November 18, 2001, at 02:20  PM, Matthias Gall wrote:
I just asked myself whether it's possible to have something like the vararg
function for function results: a function returns a variable number of
values. I would be happy about a short code snippet, if this feature is
supported. I am using LUA 4.0.

function foo()
  return 6, "hello", {}, 32
end

local  a, b, c, d = foo()

Steve



Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Matthias Gall
> function foo()
>    return 6, "hello", {}, 32
> end
>
> local  a, b, c, d = foo()
>
> Steve

Thank you, Steve. Unfortunately, this is not a variable number of return
values. I am searching for a way to return one time 1, another time 2, ...,
or even 100 values without a need to bother about the size of the
destination.

Some pseudo code:
Result = Foo()
NumValues = sizeof(Result)
FirstValue = Result[0]
LastValue = Result[NumValues-1]

Is something like this possible in LUA?

Greetings,
Matt


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Siome Klein Goldenstein
On Sun, Nov 18, 2001 at 11:38:50PM +0100, Matthias Gall wrote:
> > function foo()
> >    return 6, "hello", {}, 32
> > end
> >
> > local  a, b, c, d = foo()
> >
> > Steve
> 
> Thank you, Steve. Unfortunately, this is not a variable number of return
> values. I am searching for a way to return one time 1, another time 2, ...,
> or even 100 values without a need to bother about the size of the
> destination.
> 
> Some pseudo code:
> Result = Foo()
> NumValues = sizeof(Result)
> FirstValue = Result[0]
> LastValue = Result[NumValues-1]
> 
> Is something like this possible in LUA?
> 
> Greetings,
> Matt


function foo ()
  return {6, "hello", {}, 32}
end

Result = foo()
NumValues = getn(Result)
FirstValue = Result[1]
LastValue = Result[NumValues]


-- 
----------------------------
Siome Klein Goldenstein
[hidden email]
http://www.cis.upenn.edu/~siome

Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Matthias Gall
> function foo ()
>   return {6, "hello", {}, 32}
> end
> 
> Result = foo()
> NumValues = getn(Result)
> FirstValue = Result[1]
> LastValue = Result[NumValues]

Okay, so, it was that easy. Thank you very much.

Greetings,
Matt


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Matthias Gall
So, I tried this with a C function, which is registered properly. It pushes
some random values and returns the number of values (lua_pushnumber(MyState,
iRandomNumber); ... return iNumOfNumbers;). Using getn on the result of this
function leads to a LUA_ERRRUN. Any suggestions?

Greetings,
Matt


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Edgar Toernig
In reply to this post by Matthias Gall
Matthias Gall wrote:
> 
> I just asked myself whether it's possible to have something like the vararg
> function for function results: a function returns a variable number of
> values. I would be happy about a short code snippet, if this feature is
> supported. I am using LUA 4.0.

Is this short enough? *g*

  function pack(...)
    return arg
  end


Example:

  function func1() return end
  function func2() return 1,"foo",3 end
  function func3() return {} end

  x = pack(func1())
  foreach(x, print)	--> n=0

  x = pack(func2())
  foreach(x, print)	--> n=3  [1]=1  [2]="foo"  [3]=3

  x = pack(func3())
  foreach(x, print)	--> n=1  [1]=table:xxx

Ciao, ET.

Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Luiz Henrique de Figueiredo
In reply to this post by Matthias Gall
> 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

Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Thiago Conde Figueiro-2
In reply to this post by Matthias Gall
On Mon, 19 Nov 2001 01:11:38 +0100, "Matthias Gall" <[hidden email]> wrote:

MG> So, I tried this with a C function, which is registered properly. It pushes
MG> some random values and returns the number of values (lua_pushnumber(MyState,
MG> iRandomNumber); ... return iNumOfNumbers;). Using getn on the result of this
MG> function leads to a LUA_ERRRUN. Any suggestions?

	Yes.  What getn() really does it look up the table for a field named "n".

>   return {6, "hello", {}, 32}

	No field name 'n' on that table, so getn() fails.  Instead you should do something like

function foo()
local RetVal = {}
while (getting return values) do
	tinsert(RetVal, ReturnValueN)
end
return RetVal
end

	Now getn(foo()) will work.  I think. :)

Regards,

-- 
Thiago Conde Figueiró
Infraestrutura
Cipher Technology
http://www.ciphertech.com.br/

_____
"Segurança em TI - uma especialidade Cipher Technology"

   
 
   


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Reuben Thomas-4
> 	Yes.  What getn() really does it look up the table for a field named "n".

No, getn actually counts the number of numeric fields if there's no n
field.


Reply | Threaded
Open this post in threaded view
|

RE: Return a variable number of values from function

jmckenn
In reply to this post by Matthias Gall
Thiago Conde Figueiro writes in reply to Matthias Gall:

>> So, I tried this with a C function, which is registered 
>> properly. It pushes some random values and returns the
>> number of values (lua_pushnumber(MyState, iRandomNumber);
>> ...
>> return iNumOfNumbers;). Using getn on the result of this
>> function leads to a LUA_ERRRUN. Any suggestions?

>	Yes.  What getn() really does it look up the table for 
>a field named "n".
>
>>   return {6, "hello", {}, 32}
>
>	No field name 'n' on that table, so getn() fails.  

No - getn returns the value of the field named "n" if it exists.  If there
is no field called "n", it returns the largest numerical index with a
non-nil value in the table.

Matthias should be pushing a single value (a table), instead of multiple
individual values and a count.  Or he could push the multiple values and
count and use Edgar Toernig's pack() function (very nifty, btw!).
-Virus scanned and cleared ok

Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Oscar Lazzarino
In reply to this post by Reuben Thomas-4
Reuben Thomas wrote:
> 
> >       Yes.  What getn() really does it look up the table for a field named "n".
> 
> No, getn actually counts the number of numeric fields if there's no n
> field.

Probably getn() fails because you didn't do a lua_baselibopen in your C
program.

Oscar Lazzarino

Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Eduardo Ochs-2
In reply to this post by Matthias Gall
> > 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/
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Luiz Henrique de Figueiredo
In reply to this post by Matthias Gall
>So, I tried this with a C function, which is registered properly. It pushes
>some random values and returns the number of values (lua_pushnumber(MyState,
>iRandomNumber); ... return iNumOfNumbers;). Using getn on the result of this
>function leads to a LUA_ERRRUN. Any suggestions?

If you're returning the values by themselves, not inside a table, then
your code should be right (lua_pushnumber, lua_pushnumber, ... return n)
In this case, you cannot use getn because no table is returned.

If you're using a table, then you should return 1 not n, because the table is
the only return value.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Matthias Gall
In reply to this post by jmckenn
> Matthias should be pushing a single value (a table), instead of multiple
> individual values and a count.  Or he could push the multiple values and

I read 5.10 and 5.11 in the manual several times now, but it seems there's
nothing about creating a table from the C API! lua_settable works with a
"table that resides somewhere in the stack", and requires a key pushed onto
the stack - what key (I just want to return a random number of numerical
values from my C function to the LUA script)? lua_rawseti does not seem to
be the right method, without having created a table somehow.

Greetings,
Matt


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Terence Martin-3
lua_newtable(L) creates a table and puts it on the stack. Then you can use
lua_settable to put stuff into it. Pretty sure this is in the
documentation, since I know it and I haven't perused much of the lua code. 

On Mon, 19 Nov 2001 20:52:36 +0100
"Matthias Gall" <[hidden email]> wrote:

> > Matthias should be pushing a single value (a table), instead of
multiple
> > individual values and a count.  Or he could push the multiple values
and
> 
> I read 5.10 and 5.11 in the manual several times now, but it seems
there's
> nothing about creating a table from the C API! lua_settable works with a
> "table that resides somewhere in the stack", and requires a key pushed
onto
> the stack - what key (I just want to return a random number of numerical
> values from my C function to the LUA script)? lua_rawseti does not seem
to
> be the right method, without having created a table somehow.
> 
> Greetings,
> Matt
> 

Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Diego Nehab-3
In reply to this post by Matthias Gall
> > Matthias should be pushing a single value (a table), instead of multiple
> > individual values and a count.  Or he could push the multiple values and
> 
> I read 5.10 and 5.11 in the manual several times now, but it seems there's
> nothing about creating a table from the C API! lua_settable works with a
> "table that resides somewhere in the stack", and requires a key pushed onto
> the stack - what key (I just want to return a random number of numerical
> values from my C function to the LUA script)? lua_rawseti does not seem to
> be the right method, without having created a table somehow.

Hi,

In section 5.10 :-)

"Finally, the function void lua_newtable (lua_State *L);
creates a new, empty table and pushes it onto the stack."

[]s,
Diego.


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Matthias Gall
> In section 5.10 :-)
>
> "Finally, the function void lua_newtable (lua_State *L);
> creates a new, empty table and pushes it onto the stack."

I should have more sleep. Sorry for bothering your time, hope you had a
laugh.

Greetings,
Matt


Reply | Threaded
Open this post in threaded view
|

Re: Return a variable number of values from function

Luiz Henrique de Figueiredo
In reply to this post by Matthias Gall
>I read 5.10 and 5.11 in the manual several times now, but it seems there's
>nothing about creating a table from the C API!

At the very end of 5.10, the manual of Lua 4.0 says:

   Finally, the function
       void lua_newtable (lua_State *L);
   creates a new, empty table and pushes it onto the stack.

--lhf