kjpolker Posted July 2, 2023 Share Posted July 2, 2023 (edited) Is there a reason I cannot get my script to Evaluate a function with arrays? I have tried assigning the full function in a variable and evaluating that variable... #include <AutoItConstants.au3> #include <GUIConstants.au3> HotKeySet("^{NUMPAD1}", "test") HotKeySet("^{NUMPAD2}", "test") gui() Func gui() Global $hGUI = GUICreate("Test", 100, 100) GUISetState() While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd GUIDelete() EndFunc Func test() Local $test1[2] = ["Text 1", "Text 2"] Local $test2[2] = ["Text 3", "Text 4"] ConsoleWrite(Eval("$test" & StringRegExpReplace(@HotKeyPressed, "\D+", "")[0]) & @CRLF & Eval("$test" & StringRegExpReplace(@HotKeyPressed, "\D+", "")[1]) & @CRLF) EndFunc Note that the hotkeys are with CTRL. Regular expression is to extract the numeric value out of the @HotKeyPressed. I have tested the entire string and it is being built accurately, it just won't display the values. It simply closes, and with previous tweaking it was evaluating to an empty string. Edited July 2, 2023 by kjpolker Link to comment Share on other sites More sharing options...
Solution mikell Posted July 2, 2023 Solution Share Posted July 2, 2023 (edited) In Eval don't put the '$' before the variable name #include <AutoItConstants.au3> #include <GUIConstants.au3> HotKeySet("^{NUMPAD1}", "test") HotKeySet("^{NUMPAD2}", "test") gui() Func gui() Global $hGUI = GUICreate("Test", 100, 100) GUISetState() While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd GUIDelete() EndFunc Func test() Local $test1[2] = ["Text 1", "Text 2"] Local $test2[2] = ["Text 3", "Text 4"] $n = StringRegExpReplace(@HotKeyPressed, "\D+", "") $a = Eval("test" & $n)[0] $b = Eval("test" & $n)[1] Msgbox(0,"", $a & @CRLF & $b) EndFunc Edit and if you absolutely want to use a one-liner then take care of the parenthesis ConsoleWrite((Eval("test" & StringRegExpReplace(@HotKeyPressed, "\D+", ""))[0]) & @CRLF & (Eval("test" & StringRegExpReplace(@HotKeyPressed, "\D+", ""))[1]) & @CRLF) Edited July 2, 2023 by mikell kjpolker 1 Link to comment Share on other sites More sharing options...
water Posted July 2, 2023 Share Posted July 2, 2023 N.B. And because Eval evaluates a variable by Name you have to access the variable/array content by index after the evaluation. kjpolker 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...
mikell Posted July 2, 2023 Share Posted July 2, 2023 4 minutes ago, water said: you have to access the variable/array content by index after the evaluation That's it. Parenthesis are very helpful keys Link to comment Share on other sites More sharing options...
kjpolker Posted July 2, 2023 Author Share Posted July 2, 2023 Wow I really appreciate it. I was loosing hair from scratching my head so much not sure how I missed the '$' not being required, probably because most links and help file don't use a literal string but rather a variable which does require the prefix. Also great to know about the array index being after Eval! I wasn't sure on that one and I kept flipping it around every time I tweaked it, not a ton of clarification available (at least provided by my search words). 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