Jump to content

Eval() with Array Variables


Go to solution Solved by mikell,

Recommended Posts

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 by kjpolker
Link to comment
Share on other sites

  • Solution

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 by mikell
Link to comment
Share on other sites

N.B. And because Eval evaluates a variable by Name you have to access the variable/array content by index after the evaluation.

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

Wow I really appreciate it. I was loosing hair from scratching my head so much :P 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

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...