Jump to content

Does autoit have a reflection mechanism to return variable names as strings?


Recommended Posts

@Subz Even with your code I get 5 as a result.

I suggest you have a look at cDebug that does what you are looking for.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@Water, @JockoDundee, sorry missed the required output, I blame my seeing eye dog. 

Maybe something like this?

$n1Var = 5

$s1Var = _GetName("n1Var")
ConsoleWrite($s1Var & (@error = 0 ? " defined variable" : " undefined variable") & @CRLF)
$s2Var = _GetName("n2Var")
ConsoleWrite($s2Var & (@error = 0 ? " defined variable" : " undefined variable") & @CRLF)

Func _GetName($_vVar)
    Local $vVar = Eval($_vVar)
    Local $iError = @error
    Return SetError($iError, 0, $_vVar)
EndFunc

 

Link to comment
Share on other sites

@Subz: that wasn't the question either. ;)

As I understand it, the OP wants to know if an AutoIt function exists that returns the parsed variable's name (without necessarily knowing it's possible name(s) in advance). The answer to that question (AFAIK) is no.

Link to comment
Share on other sites

Wondering what could the real-world use case for that.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

11 hours ago, JockoDundee said:

I am aware of the Execute, Eval, Assign and IsDeclared functions.

However, going in reverse, is there a function (or group of functions) that works like the invented GetName() function does here?

Local $nVar = 5

Local $sVar = GetName($nVar)

ConsoleWrite($sVar)

Output:

nVar

With all due respect, either I am missing something or your quest to find a function or routine that passes back the name of the variable passed to it, makes no sense.  :huh2: 

Speaking strictly in relation to AutoIt scripts, can you provide a single example of where you wouldn't already know the variable's name that you'd be trying to retrieve?  In your example, you're explicitly passing $nVar, which means at the time you are passing the variable to your function, you already know the name of that variable ("$nVar").  Even if you were passing the variable using an Eval(), the expression passed to the Eval() would have to resolve to the variable's name.  Meaning, again, you already know the variable's name.

I understand the need for reflection/introspection in object-oriented languages and have used it in some of the applications that I've written.  Most of the times that I've used it, were to determine the exception class name of an exception object when logging debug information for unhandled exceptions that were raised or to get parameter information for a class's method.  However, I can't think of a single time in which I used it to retrieve the name of a variable.

At the moment, the only way that I can think of to do what you're trying to do in AutoIt, would be if all of your variable were objects and all of those objects had at least 2 properties, name and value.  But if you are going to go that far, you probably would want to add a property for data type also.  :)

Edited by TheXman
Link to comment
Share on other sites

3 hours ago, jchd said:

Wondering what could the real-world use case for that.

So, one thing I end up doing a lot is printing out variables and their values to debug.  As @water has pointed out there are solutions available to help this.

However, being somewhat old school and because the size of my project may not be worth me learning to use a custom UDF just for the purpose of debugging, I have ended up just doing it the hard way.

it was just a thought that maybe I could write a function that could print out the value AND identifier as perhaps as order pair by just referencing the variable.

For instance, in a perfect world you could just say 

$vFilePathToNowhere="C:\ABC\DEF"

CustomWrite($vFilePathToNowhere)

and it would output perhaps

vFilePathToNowhere=C:\ABC\DEF

which would save me the hassle of have to write out the variable name and a string to identify it and concatenation operations etc.

does that make sense?

Edit: also for @xman

Edited by JockoDundee
x

Code hard, but don’t hard code...

Link to comment
Share on other sites

17 minutes ago, JockoDundee said:

For instance, in a perfect world you could just say 

$vFilePathToNowhere="C:\ABC\DEF"

CustomWrite($vFilePathToNowhere)

In an almost perfect world, you could just say:

$vFilePathToNowhere="C:\ABC\DEF"

CustomWrite("vFilePathToNowhere")

.
.
.

Func CustomWrite($sVarName)
    ConsoleWrite($sVarName & " = " & Eval($sVarName) & @CRLF)
EndFunc

*Untested

 

 

 

LOL....You beat me to it  :D  We must have hit enter within a few seconds of each other.

Edited by TheXman
Link to comment
Share on other sites

Just now, TheXman said:

In an almost perfect world, you could just say:

$vFilePathToNowhere="C:\ABC\DEF"

CustomWrite("vFilePathToNowhere")

.
.
.

Func CustomWrite($sVarName)
    ConsoleWrite($sVarName & " = " & Eval($sVarName) & @CRLF)
EndFunc

*Untested

 

I like your indentation and @CRLF 

much neater than mine :)

Code hard, but don’t hard code...

Link to comment
Share on other sites

FYI:  The user "Xman" is probably wondering why you keep tagging him.  I am @TheXman  :D

Link to comment
Share on other sites

@Subz, @TheXman, so I tried to use the Eval approach.

It works, BUT only with Local variables.

so I came up with this less elegant, but fully functional replacement.  Please feel free to improve it, especially if you are one of this RegEx mavens who can replace the for loop in a single statement:)

pragma compile(Console,True)

RunTest()
;Run this way so as to leave no doubt about variable visibility/scope

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func RunTest()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Local $rooms,$floors

$rooms=6
$floors=8

ConsoleWrite(Execute(PreEx("$rooms,$floors")))

EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func PreEx($str)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Local $outstr="", $pos, $stok=StringSplit($str,",")

For $n=1 To $stok[0]
    $pos=StringInStr($stok[$n],"=")
    If $pos Then $stok[$n]=StringLeft($stok[$n],$pos-1)
    $outstr&="' "&  $stok[$n] &"=' &"&$stok[$n] &"&"
Next
Return $outstr &"@CRLF"

EndFunc

 

Output: $rooms=6 $floors=8

btw, the actual real world debug statement that caused me to look for some thing dynamic was:

LogIt("mty="& $move[$mty] &" $mfx="& $move[$mfx] &"$ mvid="& $move[$mvid] &" $mv="& $move[$mv] &" $mv2="& $move[$mv2] &" $mrp="& $move[$mrp] &" $mrx="& $move[$mrx] &" $mry="& $move[$mry] &" $mrlx="& $move[$mrlx] &" $mrly="& $move[$mrly] &" $mlx="& $move[$mlx] &" $mly="& $move[$mly] &" $mpmh="& $move[$mpmh] &" $mhid="& $move[$mhid] &" $mmd="& $move[$mmd] &" $mdly="& $move[$mdly])

now I just cut the whole line from the variable declaration, and paste it as a string into the PreEx() function, and it’s so much easier...

Code hard, but don’t hard code...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...