 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
renskav Guest
|
Posted: Sun Nov 01, 2009 6:56 pm Post subject: Returning multiple values from a function? |
|
|
I want to write a function that returns five names, how to best accomplish this? I'm a bit confused about how to return multiple values from a function.
- I can't return an array since the return statement only takes one parameter.
- I've tried using global variables and having the function setting them, but since i want multi-instancing of my AHK script this approach gets sloppy/doesn't work.
- I could concatenate the five names into a colon-delimited string and then loop-parse it after i've called the function, but this seems rather messy.
Thanks in advance /renskav |
|
| Back to top |
|
 |
tonne
Joined: 06 Jun 2006 Posts: 1651 Location: Denmark
|
Posted: Sun Nov 01, 2009 7:15 pm Post subject: |
|
|
Use ByRef, as:
| Code: | return5name(A,B,C,D,E)
msgbox %A%, %B%, %C%, %D%, %E%
return
return5name(byref name1,byref name2,byref name3,byref name4,byref name5)
{
name1 := "A"
name2 := "100"
name3 := "1.1"
name4 := A_DDDD
name5 := "B"
} |
_________________ RegEx Powered Dynamic Hotstrings
COM
AutoHotkey 2 |
|
| Back to top |
|
 |
tank
Joined: 21 Dec 2007 Posts: 3700 Location: Louisville KY USA
|
Posted: Sun Nov 01, 2009 7:24 pm Post subject: |
|
|
byRef
example
| Code: | byrefoutfirst=data
msgbox % myfunc(byrefoutfirst,byrefoutsecond,byrefoutthird,byrefoutfourth,byrefoutfifth)
MsgBox but outgoing can be data of another %byrefoutfirst%
myfunc(ByRef thisval,ByRef thatval,ByRef someotherval,ByRef theval,ByRef ohandthisval)
{
msgbox byrefs can accept incomming %thisval%
thisval=type
thatval:=thatval ? false : true
someotherval:=someotherval ? false : true
theval:=theval ? false : true
ohandthisval:=ohandthisval ? false : true
Return "this is a return value"
} | by the way the whole point of arrays is to pass multiple values with one variable _________________
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Last edited by tank on Sun Nov 01, 2009 7:25 pm; edited 1 time in total |
|
| Back to top |
|
 |
renksav Guest
|
Posted: Sun Nov 01, 2009 7:24 pm Post subject: |
|
|
Ah, perfect, thanks.  |
|
| Back to top |
|
 |
renskav Guest
|
Posted: Sun Nov 01, 2009 7:26 pm Post subject: |
|
|
| by the way the whole point of arrays is to pass multiple values with one variable <- I didn't quite follow this though, im still getting used to the arrays in ahk so i dont see how i could use one to pass multiple values? |
|
| Back to top |
|
 |
tank
Joined: 21 Dec 2007 Posts: 3700 Location: Louisville KY USA
|
Posted: Sun Nov 01, 2009 8:51 pm Post subject: |
|
|
perhaps this snippet of nonsensical but working code will help spark ideas
| Code: | d0=3
d1=dgfsdf
d2=referb
d3=wefewf
arg("d")
ExitApp
arg(ar)
{
Loop,% %ar%0
{
MsgBox % %ar%%A_Index%
}
} |
_________________
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed; |
|
| Back to top |
|
 |
renskav Guest
|
Posted: Sun Nov 01, 2009 9:21 pm Post subject: |
|
|
| I see how it is intended to work, that's sort of what i was doing before.. Doing that approach created problems with using global variables when my script was running multi instance - the instances keep interfering with eachothers variables. |
|
| Back to top |
|
 |
Rabiator
Joined: 17 Apr 2005 Posts: 289 Location: Sauerland
|
Posted: Mon Nov 02, 2009 9:53 pm Post subject: |
|
|
Another and simpler way is to return a string containing the separated return values:
| Code: | a := PolarToCartesian(5, 0.2048328 * 3.1415926535)
StringSplit, array, a, |
MsgBox x = %array1%`ny = %array2%
; or in one line:
RegExMatch(PolarToCartesian(5, 0.2048328 * 3.1415926535), "(.*?)\|(.*)", array)
MsgBox x = %array1%`ny = %array2%
; with named variables:
RegExMatch(PolarToCartesian(5, 0.2048328 * 3.1415926535), "(?<x>.*?)\|(?<y>.*)", Coord_)
MsgBox x = %Coord_x%`ny = %Coord_y%
PolarToCartesian(r, phi)
{
x := r * Cos(phi)
y := r * Sin(phi)
Return x "|" y
} |
__________________________________________
Created with BBCodeWriter 7.0 - the one and only  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|