lua-users home
lua-l archive

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


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