Page 1 of 1

Drop some results

Posted: Sun Nov 10, 2019 2:23 pm
by lazi
If only one value needed from a set of the returned values by a function what is the best approach?

For example, you need the third return value of a function, you can do like this:

a,b,c,d = Manyvalues()
DebugPrint(c)
a,b,d = Nil

Not an elegant solution to use three needless variables.

A creative method could be used liked this:

c = Pack ( Manyvalues() )[2]
DebugPrint(c)

Don't know which one is more optimal by speed and memory, but the latter looks better for the eye :)

Is there any other solution? Maybe something from the Lua syntax?

Re: Drop some results

Posted: Sun Nov 10, 2019 2:58 pm
by LarsB
Why do you set a,b,d to NIL if they are not needed?

Re: Drop some results

Posted: Sun Nov 10, 2019 3:37 pm
by SamuraiCrow
LarsB wrote:
Sun Nov 10, 2019 2:58 pm
Why do you set a,b,d to NIL if they are not needed?
That's to deallocate them.

Re: Drop some results

Posted: Sun Nov 10, 2019 4:17 pm
by Bugala
Interesting problem. I actually encounter that same problem every now and then, but so far it havent been speed critical, and I have only had usually 2 max 3 values coming, so I have simply used local a,b to store the values, in which case Hollywood will itself take care of removing the a (and b as well).

But indeed, it might not be a bad idea to have some exact and fast way to access only one of the coming values.

Re: Drop some results

Posted: Sun Nov 10, 2019 7:06 pm
by p-OS
I wonder why you assign the values at all if you don't need them ?

This will work:

Code: Select all

myfunc=Function()
 Return(1,2,3,4)
EndFunction

a,b,c=myfunc()
DebugPrint(a)
DebugPrint(b)
DebugPrint(c)
DebugPrint(d)
This, too:

Code: Select all

myfunc=Function()
 Return(1,2,3,4)
EndFunction

a,b,c,d,e=myfunc()
DebugPrint(a)
DebugPrint(b)
DebugPrint(c)
DebugPrint(d)
DebugPrint(e)

Re: Drop some results

Posted: Sun Nov 10, 2019 8:44 pm
by jPV
lazi wrote:
Sun Nov 10, 2019 2:23 pm
If only one value needed from a set of the returned values by a function what is the best approach?

For example, you need the third return value of a function, you can do like this:

a,b,c,d = Manyvalues()
DebugPrint(c)
a,b,d = Nil

Not an elegant solution to use three needless variables.
I usually just create local variables, and this kind of stuff is usually used in some code block/function, so they're deleted automatically then.

Code: Select all

local a,b,c,d = Manyvalues()
Debugprint(c)
If used in the main level of the code, then I set them to Nil like you, but it's quite rare... I would guess using extra function like Pack would be slower.

Re: Drop some results

Posted: Mon Nov 11, 2019 9:52 am
by lazi
lazi wrote:
Sun Nov 10, 2019 2:23 pm

Code: Select all

a,b,c,d = Manyvalues()
Debugprint(c)
a,b,d = Nil
This seems to be the best way, but it can be optimized a bit further:

Code: Select all

a, a, c, a = Manyvalues()
Debugprint(c)
a = Nil
Checked in the Lua world and only a select() function can be find for similar problems, but that is not needed here.
(http://www.lua.org/manual/5.1/manual.html#pdf-select)