## Drop some results

Find quick help here to get you started with Hollywood
lazi
Posts: 553
Joined: Thu Feb 24, 2011 11:08 pm

### Drop some results

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: 69
Joined: Sat May 06, 2017 4:37 pm

### Re: Drop some results

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

SamuraiCrow
Posts: 332
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

### Re: Drop some results

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.
I'm on registered MorphOS using FlowStudio.

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

### Re: Drop some results

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: 124
Joined: Mon Nov 01, 2010 11:56 pm

### Re: Drop some results

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)
``````

jPV
Posts: 365
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

### Re: Drop some results

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.

lazi
Posts: 553
Joined: Thu Feb 24, 2011 11:08 pm

### Re: Drop some results

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)