• Subject: Re: Multi-value functions and tables
• From: Hans Ole Rafaelsen <hansr@...>
• Date: 26 Jun 2001 18:35:51 +0200

```Thanks Erik!

Use of "call" helps me somewhat.  However, I end up with a level of
indirection.  I have to creat a "dummy" value in the table, which holds
the table returned by the function.  This is probably a better way
than my first solution, which was to have to functions returning each
of the results.  Guess that would have required evaluation of the
function twice, while this only requires one, on the expense of some
memory usage.

tab2 = {
attr1 = fun1(tab1.attr1),
dummy = call(fun2, {tab1.attr1}, "p"),
attr2 = tab2.dummy[1],
attr3 = tab2.dummy[2],
}

would like to write:

tab2 = {
attr1 = fun1(tab1.attr1),
attr2, attr3 = fun2(tab1.attr1)
}

The two values returned are assigned, the same way as you can inside
functions.  But since "," is used as a separator when construction the
table, it seems like Lua is not able to handle this kind of
expressions.

Guess I'll have to live with the "dummy" then.

Regards,

Hans Ole

erik@hougaard.com (Erik Hougaard) writes:

> Hi Hans Ole,
>
> Please take a look the "call" function!
>
> /Erik
>
> ----- Original Message -----
> From: "Hans Ole Rafaelsen" <hansr@cs.uit.no>
> To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
> Sent: Tuesday, June 26, 2001 1:54 PM
> Subject: Multi-value functions and tables
>
>
> > Hello,
> >
> > I'm wondering if it is possible to store the result of a function
> > returning multiple values in tables.
> >
> > Given the following functions:
> >
> > function fun1(arg)
> >   return arg * 2
> > end
> >
> > function fun2(arg)
> >   local r1, r2
> >
> >   r1 = arg * 2
> >   r2 = arg * 3
> >
> >   return r1, r2
> > end
> >
> >
> > and the following tabels:
> >
> > tab1 = {
> >   attr1 = 2
> > }
> >
> > tab2 = {
> >   attr1 = fun1(tab1.attr1),
> >   attr2, attr3 = fun2(2)  --   <----  This results in an error!
> > }
> >
> > attr2, attr3 = fun2(2)    --   <----  While this is a legal statement
> >
> >
> > The problem is to store both values returned from fun2() in tab2.  The
> > problem seems to be the use of "," to sperate elements (attr2 and
> > attr3) in the table.  I have search the documentation, but I have not
> > been able to find a way to make it work the same way for tables as it
> > does for the code outside tables.  Is there a way to do this, or do I
> > have to re-write my functions?
> >
> > Regards,
> >
> > Hans Ole Rafaelsen
> >
> >
> >

```