Jump to content
OldGuyWalking

Capturing the Name of a Variable

Recommended Posts

OldGuyWalking

Is there a way within AutoIt to capture the name of a variable when it's value is passed to a function as a parameter? 

I have various "debug" functions I use that I can pass the FuncName, ScriptName, ScriptLineNumber, and other info to but I have no idea how to pass a variables name to a function other than by manually assigning it to another variable. 

For example, below, in order to show the arrays variable "name" I pass it to the function through the $sArrayName variable. 

; #FUNCTION# ====================================================================================================================
; Name ..........: _Debug_WriteArrayToConsole
; Description ...: Writes a 1 or 2 dimensional array values to the console
; Syntax ........: _Debug_WriteArrayToConsole(Byref $aArray[, $sArrayName = "Array"])
; Parameters ....: $aArray              - 1 or 2 dimensional array of unknowns.
;                  $sArrayName          - [optional] The name of the array. Default is "Array".
;                  $sInfoText           - [optional] Information from user.
; Return values .: None
; Author ........: OldGuyWalking
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _Debug_WriteArrayToConsole(ByRef $aArray, $sArrayName = "Array", $sInfoText = "")
    Local $hFunc = _Debug_WriteArrayToConsole
    Local $sMsg
    Local $sMsgHdr
    Local $iErrCode
    Local $iDimensions
    Local $iColumns
    Local $n1
    Local $n2

    If _IsValueEmpty($aArray) Then
        ;############### MSG2 - START ###############
        $sMsgHdr = FuncName($hFunc) & " :Line: " & @ScriptLineNumber & " :Error= " & @error
        $sMsg = "Not an Array."
        MsgBox(0, $sMsgHdr, $sMsg)
        Return SetError(1, 0, "")
        ;############### MSG2 - END ###############
    EndIf

    $iDimensions = UBound($aArray, $UBOUND_DIMENSIONS)
    $iColumns = UBound($aArray, $UBOUND_COLUMNS)

    ;############### CONSOLEWRITE - START ###############
    ConsoleWrite($gdblLine132 & @CRLF)
    ConsoleWrite(" Line Number= " & @ScriptLineNumber & @CRLF)
    ConsoleWrite(" $hFunc     = " & FuncName($hFunc) & @CRLF)
    ConsoleWrite(" Array Name = " & $sArrayName & @CRLF)
    ConsoleWrite(" Other Info = " & $sInfoText & @CRLF)
    ConsoleWrite(" Dimensions = " & $iDimensions & @CRLF)
    ConsoleWrite(" Columns    = " & $iColumns & @CRLF)
    ConsoleWrite(" Rows       = " & UBound($aArray) - 1 & @CRLF)
    ConsoleWrite($gsngLine132 & @CRLF)
    For $n1 = 0 To UBound($aArray) - 1
        $sArrayName &= "[" & $n1 & "]"
        If $iDimensions = 1 Then
            $sArrayName &= " = "
            ConsoleWrite($n1 & " of  " & UBound($aArray) - 1 & " - " & $aArray[$n1] & @CRLF)
        EndIf
        If $iDimensions = 2 Then
            ConsoleWrite($n1 & " of  " & UBound($aArray) - 1 & @CRLF)
            For $n2 = 0 To $iColumns
                $sArrayName &= "[" & $n1 & "][" & $n2 & "] = "
                ConsoleWrite(@TAB & $n2 & " of  " & $iColumns & " - " & $sArrayName & $aArray[$n1][$n2] & @CRLF)
            Next
        EndIf
    Next
    ConsoleWrite($gdblLine132 & @CRLF)
    ConsoleWrite(@CRLF)
    ;############### CONSOLEWRITE - END ###############
EndFunc   ;==>_Debug_WriteArrayToConsole

Thanks in advance for any answers.

OldGuyWalking

Share this post


Link to post
Share on other sites
jpm
Posted (edited)

the only thing you can do is pass the variable name as a string and in the func use

$aArray = Eval($sVarname)

Edited Execute wrong  Eval better

Edited by jpm
Edited Execute wrong Eval better
  • Like 1

Share this post


Link to post
Share on other sites
Earthshine

yeah, there is no Reflection as with Python and C# and other languages. I make it a habit to log all the parameters of every function call up front so the log shows it all nicely documented.


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
OldGuyWalking

Thanks for the responses.

JPM - Thank you. I'll look at Assign and Eval again.  I'd looked at them before but they didn't seem to fit what I was looking for but will reread.

Earthshine -  I agree. I use various #include files, including a few that I've built, and writing to logs is the only way I've found I can locate some logic errors in code when the app jumps in and out of multiple functions across several include files.  

OldGuyWalking

 

  • Like 1

Share this post


Link to post
Share on other sites
junkew

misinterpreted your question but here an example direction that you could try to analyze your script (some kind of preparsing with regex)

combining it with wrapped function thru a variable you can do a lot but it can become terribly complex

I wouldn't be surprised if its possible somewhere hidden in the code as we have call("functionname as as string"), eval, assign, $var=function it does not sound unlogical to have the feature you are looking for but initially I would think you have to preparse your code somehow (Regex is quick but could bring you into many problems)

;~ Small example of 
;~ assigning function to variable
;~ regexing your coding, see oope.au3 in forum for more advanced regex on au3 coding
;~    eval/assign  could be helpfull

#include <StringConstants.au3>
$debugF=f1
$debugF(2)

$debugF=f2
$debugF(2,2)

$a=3
$b=3
$debugF($a,$b)

$debugF=debug_f2
$debugF(2,2)

$myCode=fileread(@scriptfullpath)
$aArray=StringRegExp($myCode,"[A-Za-z]*?\(.*?\)", $STR_REGEXPARRAYGLOBALMATCH  )
For $i = 0 To UBound($aArray) - 1
    consolewrite($i & $aArray[$i] &@CRLF)
Next

func f1($x)
    consolewrite($x &@CRLF)
EndFunc

func f2($y,$z)
    consolewrite($y+$z &@CRLF)
EndFunc

func debug_f2($p1,$p2)
    consolewrite("Just wrapped and now calling f2" &@CRLF)
    call("f2", $p1, $p2)
    consolewrite("End calling f2" &@CRLF)
EndFunc

In oope.au3 an amazing job was done within AutoIt some preparsing with regex finding

 

 

Share this post


Link to post
Share on other sites
OldGuyWalking
5 minutes ago, junkew said:

but initially I would think you have to preparse your code somehow (Regex is quick but could bring you into many problems)

After looking through the help files and going through the forums over the past few months I hadn't found a way to capture the variable name.  It's not a major deal, more like having a 100 piece puzzle with one piece missing and it was bugging me. 

I'm going to apologize now if the following is too much detail but while I'm pretty sure the people who responded understand what I'm looking for, and that AutoIt doesn't have this particular feature,  I'm not sure if I was clear that I am looking for results to be displayed in real time. This is just an additional explaination of what I'm doing so I don't really have a question in here.

To start, if it helps, I'm looking for the same type of real time results one would get if they highlighted a variable, pressed Alt+D (Debug to Console) in SCite, and then ran their code. I'm used to debuggers that let me step through code and across functions (regardless of where the function is located) and to pick a variable or variables and track it from start to finish.  The closest thing I found that works for me is writing information to the console.

For my own convenience, I've just expanded and dressed up the Debug to Console concept a bit. Also, where possible, I want as much "debug" functionality pushed into a separate include file.  It isn't practical in all cases but, in some cases it works. For example, when I want to see array values but don't want to stop the application, I'll use the _debug_WriteArrayToConsole which is in an include file so, like _ArrayDisplay, it's just a one line function call.

I use the User Abbreviations feature in SCite or PSPads Clip definitions, to add my "debug" code.  One user abbreviation is mcw (which stands for myconsolewrite).  Like Alt+D, I add it to my code to identify both the name of a variable and the variable value.  Unlike Alt+D, I use it to display many variables and variable values at one time.

{See the code below}

I populate each $var by double clicking on a variable from my code or highlighting a function call (i.e., UBound($Array) - 1), press control + C to copy, and then double click the $var to the left of the equal sign, {paste}, double click the $var to the right of the equal sign, {paste}.

Because, (1)  I use this code to debug variables within functions where most of the variables are local and (2) I may need to see from one to all of the variables in a section of code in order to figure out what's gone wrong, this particular debug code needs to be inserted into the function (or in several places in a function).

that way due to the varying number of variables I might want to track whereas code like the _Debug_WriteArraytoConsole in my original post is a function that I placed in an include file and I can call it from anywhere. 

Calls to debug functions in an include file (like _WriteArrayToConsole) is when I want to be able to pass as much information about the function, variable, name, values etc., in real time, without having to hardcode any information.   Macros and Functions like  @ScriptName, @ScriptLineNumber, FuncName, are useful because of their ability to pass the current/updated information they hold without the user having to set anything.

I hope that makes sense. 

; $gdblLine132 is a variable that holds 132 = (equal) signs.

;############### CONSOLEWRITE - START ###############
ConsoleWrite($gdblLine132 & @CRLF) ; 
ConsoleWrite("Script Name:  " & @ScriptName & @CRLF)
ConsoleWrite("Function: " & FuncName($hFunc) & @CRLF)
ConsoleWrite("Starting Line: " & @ScriptLineNumber & @CRLF)
ConsoleWrite("@error = " & $iErrCode & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite(@TAB & " $var = " & $var & @CRLF)
ConsoleWrite($gdblLine132 & @CRLF)
ConsoleWrite(@CRLF)
;############### CONSOLEWRITE - END ###############


;############### _WriteLogFile - START ###############
_WriteLogFile($gdblLine132, $gLocalLogFile)
_WriteLogFile("Script Name:  " & @ScriptName, $gLocalLogFile)
_WriteLogFile("Function: " & FuncName($hFunc), $gLocalLogFile)
_WriteLogFile("Starting Line: " & @ScriptLineNumber, $gLocalLogFile)
_WriteLogFile("@error = " & $iErrCode, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile(@TAB & " $var = " & $var, $gLocalLogFile)
_WriteLogFile($gdblLine132, $gLocalLogFile)
_WriteLogFile(" ", $gLocalLogFile)
;############### _WriteLogFile - END ###############

Thanks,

OldGuyWalking

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×