Drop some results

Find quick help here to get you started with Hollywood
Post Reply
User avatar
lazi
Posts: 363
Joined: Thu Feb 24, 2011 11:08 pm

Drop some results

Post by lazi » 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.

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?

LarsB
Posts: 53
Joined: Sat May 06, 2017 4:37 pm

Re: Drop some results

Post by LarsB » Sun Nov 10, 2019 2:58 pm

Why do you set a,b,d to NIL if they are not needed?

SamuraiCrow
Posts: 242
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Drop some results

Post by SamuraiCrow » Sun Nov 10, 2019 3:37 pm

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.

Bugala
Posts: 825
Joined: Sun Feb 14, 2010 7:11 pm

Re: Drop some results

Post by Bugala » Sun Nov 10, 2019 4:17 pm

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.

p-OS
Posts: 112
Joined: Mon Nov 01, 2010 11:56 pm

Re: Drop some results

Post by p-OS » Sun Nov 10, 2019 7:06 pm

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)

User avatar
jPV
Posts: 281
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Drop some results

Post by jPV » Sun Nov 10, 2019 8:44 pm

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.

User avatar
lazi
Posts: 363
Joined: Thu Feb 24, 2011 11:08 pm

Re: Drop some results

Post by lazi » Mon Nov 11, 2019 9:52 am

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)

Post Reply