JockoDundee Posted October 3, 2020 Share Posted October 3, 2020 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 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Subz Posted October 3, 2020 Share Posted October 3, 2020 You could just use: $sVar = $nVar ConsoleWrite($sVar & @CRLF) Link to comment Share on other sites More sharing options...
water Posted October 3, 2020 Share Posted October 3, 2020 @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. JockoDundee 1 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 More sharing options...
Subz Posted October 3, 2020 Share Posted October 3, 2020 @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 JockoDundee 1 Link to comment Share on other sites More sharing options...
RTFC Posted October 3, 2020 Share Posted October 3, 2020 @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. My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
Subz Posted October 3, 2020 Share Posted October 3, 2020 @RTFC Missed that completely , wouldn't be the first time time for zzzz. Link to comment Share on other sites More sharing options...
jchd Posted October 3, 2020 Share Posted October 3, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
JockoDundee Posted October 3, 2020 Author Share Posted October 3, 2020 7 hours ago, water said: @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. @Subz, thanks, that looks interesting, and could help beat some of the tedium... Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
TheXman Posted October 3, 2020 Share Posted October 3, 2020 (edited) 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. 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 October 3, 2020 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
JockoDundee Posted October 3, 2020 Author Share Posted October 3, 2020 (edited) 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 October 3, 2020 by JockoDundee x Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
JockoDundee Posted October 3, 2020 Author Share Posted October 3, 2020 I suppose I could just use Eval: like $vFilePathToNowhere="C:\ABC\DEF" CustomWrite("vFilePathToNowhere") and Func CustomWrite($var1) ConsoleWrite($var1 &"="& Eval($var1)) EndFunc TheXman 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
TheXman Posted October 3, 2020 Share Posted October 3, 2020 (edited) 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 We must have hit enter within a few seconds of each other. Edited October 3, 2020 by TheXman JockoDundee 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
JockoDundee Posted October 3, 2020 Author Share Posted October 3, 2020 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 More sharing options...
TheXman Posted October 3, 2020 Share Posted October 3, 2020 FYI: The user "Xman" is probably wondering why you keep tagging him. I am @TheXman JockoDundee 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Subz Posted October 3, 2020 Share Posted October 3, 2020 Isn't that what I wrote above? Link to comment Share on other sites More sharing options...
JockoDundee Posted October 3, 2020 Author Share Posted October 3, 2020 59 minutes ago, Subz said: Isn't that what I wrote above? So it is! Thx and apologies. btw, my delayed upvote has apparently pushed you out of your sinister, if cool, reputational tally Subz 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
JockoDundee Posted October 4, 2020 Author Share Posted October 4, 2020 @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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now