Single variable function returning variable name and value

Get help with using AutoHotkey and its commands and hotkeys
stepanVL
Posts: 11
Joined: 27 Jun 2017, 08:56

Single variable function returning variable name and value

03 Jan 2018, 13:49

Is it possible to write AHK function to do this?

Code: Select all

firstVariable := 34
nextVariable := 42
MsgBox %  MySpecialFunction(firstVariable)		;    returns     "firstVariable : 34"
MsgBox % MySpecialFunction(nextVariable) 		;   returns      "nextVariable: 42"
------------------------------------------------
Update
------------------------------------------------
Lets say I am debugging MyTest()

Code: Select all

MyTest()
	{
	Loop, 10
		{
    		score := getScore(a_index)
    		;  MsgBox %  "score " . score  "; index " . a_index  ; Method 1  - too many Messages
    		; acc := acc . "`r`ntest1 score: " . score   . " index " . a_index   ; Method 2
    		
   		if (score  > 80){
   			; MsgBox % "exiting test 1 - true`r`n" . acc    ;  Method 2 continued - it requires too much code too much writing required 
   			return "Test 1 : " .  score . " points after  " . a_index . " steps"   		
   			}	
   		}
   		Loop, 20
		{
    		anotherScore := getAnotherScore(a_index)
    		;  MsgBox %  "anotherScore " . anotherScore "; index " . a_index  ; Method 1  - too many Messages
    		; acc := acc . "`r`ntest1 score: " . score   . " index " . a_index   ; Method 2 ... we forgot to replace   "score" with   "anotherScore " .. missleading output
    		; MyDbg.append(anotherScore )   ; Method 3 - least clicks: just ^c^v "MyDbg.append()" then paste "anotherScore "
   		if (score  > 60){
   			; MsgBox % "exiting test 1 - true`r`n" . acc    ;  Method 2 continued   .. test 2 again
   			; MyDbg.Msg()    ;; Method 3 desired method
   			return "Test 2 : " .  anotherScore  . " points after  " . a_index . " steps"
   			}
   		}
   	; MsgBox % "exiting method" . acc  ; Method 2
   	return "Low scores in tests 1 and 2"
      }
results are off, so I want to MsgBox text like
loop1: (1,3), (2,38),..
loop2: (1,12121) ! found. Success!

I have two options :
Method 1: add two lines of MsgBox inside of the loop (fast but tones of boxes)
Method 2: use append - cleaner output, but too much typing, easy to make mistakes, leaves a lot of dead code
Method 3: fast, clean, reliable. But I can't get mapping

Code: Select all

MsgBox %  MySpecialFunction(firstVariable)		;    returns     "firstVariable : 34"
MsgBox % MySpecialFunction(nextVariable) 		;   returns      "nextVariable: 42"
Last edited by stepanVL on 03 Jan 2018, 16:32, edited 1 time in total.
Helgef
Posts: 4334
Joined: 17 Jul 2016, 01:02
Contact:

Re: Single variable function returning variable name and value

03 Jan 2018, 14:16

Not that I'm aware. For global variables you can do like this, note that it doesn't work for a local variable,

Code: Select all


firstVariable := 34
nextVariable := 42
MsgBox %  MySpecialFunction("firstVariable")		;    returns     "firstVariable : 34"
MsgBox % MySpecialFunction("nextVariable") 		;   returns      "nextVariable: 42"

f()
f(){
	v := 1
	msgbox % MySpecialFunction("v")	; NO
}

MySpecialFunction(varName){	
	return varName  " : " %varName%
}
Cheers.
nadjibSoft
Posts: 9
Joined: 29 Dec 2017, 14:57
Facebook: 100011403841640

Re: Single variable function returning variable name and value

03 Jan 2018, 14:50

Put the value and the name in array.... and return the array
clear ?
stepanVL
Posts: 11
Joined: 27 Jun 2017, 08:56

Re: Single variable function returning variable name and value

03 Jan 2018, 15:16

nadjibSoft wrote:Put the value and the name in array.... and return the array... clear ?
I know how to store multiple values in collections (Arrays in this case)

Code: Select all

; ...
VarNamesArray[3] := "variableX"
VarValuesArray[3] := variableX
; ...
My question is different though.. I've edited it.
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Single variable function returning variable name and value

03 Jan 2018, 17:39

Maybe this?:

Code: Select all

Global Variables := []
Variables["VarOne"] := 32
Variables["OtherVar"] := 16
Variables["Another"] := 8

MsgBox % TestFunc("OtherVar")

TestFunc(arg)
{
	return % arg " : " Variables[arg]
}
If not, maybe toy around ByRef and DeRef (%varName in expression)?
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
FanaticGuru
Posts: 1402
Joined: 30 Sep 2013, 22:25

Re: Single variable function returning variable name and value

03 Jan 2018, 18:02

Maybe I am missing something but this seems pretty simply if you are willing to send the variable name as a literal.

Code: Select all

firstVariable := 34
nextVariable := 42
MsgBox %  MySpecialFunction("firstVariable")		;    returns     "firstVariable : 34"
MsgBox % MySpecialFunction("nextVariable") 		;   returns      "nextVariable: 42"


MySpecialFunction(var)
{
	return var " : " (%var%)
}
If it must be MySpecialFunction(firstVariable) then you are in for a very difficult time to get the actual name of the variable ("firstVariable") that was used to pass information even when using ByRef. It is possible but probably more work than it is worth. More an academic challenge.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Google [Bot], JoeWinograd, kochxd, Nightwolf85, roikl289, swagfag, TAC109, vir2al4u and 250 guests