Jump to content

Function Equivalent of Execute()


Recommended Posts

Local $a = 1
Local $v = Execute("$a+1") ; $v is set to 2

I've been wondering if there's something similar to Execute that would work with function names, something like:

Func _IncrementNumber($thisnumber)
    Return $thisnumber + 1
EndFunc

Local $Number = 10
Local $IncrementedNumber = ExecuteFunction("_IncrementNumber", $Number) ; $IncrementedNumber becomes 11

I haven't found anything similar to "ExecuteFunction" in the docs.

Link to comment
Share on other sites

Execute takes any valid statement and executes it.

Func _IncrementNumber($thisnumber)
    Return $thisnumber + 1
EndFunc

Local $iNumber = 10
Local $iIncrementedNumber = Execute("_IncrementNumber(" & $iNumber & ")")
ConsoleWrite($iIncrementedNumber & @CRLF)

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Even this works:

Func _IncrementNumber($thisnumber)
    Return $thisnumber + 1
EndFunc

Local $iNumber = 10
AutoItSetOption("ExpandVarStrings", 1)
Local $iIncrementedNumber = Execute("_IncrementNumber($iNumber)")
ConsoleWrite($iIncrementedNumber & @CRLF)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

And here I was writing long Switch/Case combinations :P something like this:

Switch $Function
    Case "Function1"
        $ReturnValue = Function1()
    Case "Function2"
        $ReturnValue = Function2()
    Case "Function3"
        $ReturnValue = Function3()
EndSwitch

lol

 

3 minutes ago, water said:

Local $iIncrementedNumber = Execute("_IncrementNumber($iNumber)")

Wow, I love this :) Thank you, this is perfect!

Link to comment
Share on other sites

This is made possible by

AutoItSetOption("ExpandVarStrings", 1)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Also note that functions are first-class citizens in AutoIt.

ConsoleWrite(VarGetType(ConsoleWrite) & @LF)
ConsoleWrite(VarGetType(_MyFunc) & @LF)
Local $v = _MyFunc
ConsoleWrite($v() & @LF)

Func _MyFunc()
    Return("Done")
EndFunc

This way keeps you away from calling a badly formed function name, like Call("MyFunc")

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

18 hours ago, water said:

Even this works:

Func _IncrementNumber($thisnumber)
    Return $thisnumber + 1
EndFunc

Local $iNumber = 10
AutoItSetOption("ExpandVarStrings", 1)
Local $iIncrementedNumber = Execute("_IncrementNumber($iNumber)")
ConsoleWrite($iIncrementedNumber & @CRLF)

 

 

18 hours ago, water said:

This is made possible by

AutoItSetOption("ExpandVarStrings", 1)

Hi @water.

The use of ExpandVarStrings seems to be redundant ;)

Your example works event if "ExpandVarStrings" is omitted.

example code to show why:

AutoItSetOption("ExpandVarStrings", 1)
global $iNumber = 123;
ConsoleWrite("_IncrementNumber($iNumber)"&@CRLF)
ConsoleWrite("_IncrementNumber($iNumber$)"&@CRLF)

To be clear, your example works perfectly fine, just wanted to clarify that ExpandVarStrings had nothing to do with it :)

Link to comment
Share on other sites

My example

ConsoleWrite("_IncrementNumber($iNumber)"&@CRLF)

is wrong as it misses the trailing $.
ExpandVarStrings does exactly what it is supposed to do (tested using AutoIt 3.3.16.0):

AutoItSetOption("ExpandVarStrings", 0)
ConsoleWrite("_IncrementNumber($iNumber$)" & @CRLF)

returns "_IncrementNumber($iNumber$)" and

AutoItSetOption("ExpandVarStrings", 1)
ConsoleWrite("_IncrementNumber($iNumber$)" & @CRLF)

returns "_IncrementNumber(123)".

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Func _IncrementNumber($thisnumber)
    Return $thisnumber + 1
EndFunc

Local $iNumber = 10
Local $iIncrementedNumber = Execute("_IncrementNumber(" & $iNumber & ")")
ConsoleWrite($iIncrementedNumber & @CRLF)

$iIncrementedNumber = Execute("_IncrementNumber($iNumber)")
ConsoleWrite($iIncrementedNumber & @CRLF)

Both of these returned 11 for me without setting "ExpandVarStrings".

Link to comment
Share on other sites

@noellarkin

Yes both  return 11 but that is not a failure of "ExpandVarStrings" but a feature of the Execute function.

You are not using the $var$-syntax for "ExpandVarString" in your example.

If you use the "ExpandVarString"-syntax you will see the difference.

See the expanded example program below.

Local $iNumber = 10
Local $iIncrementedNumber = Execute("_IncrementNumber(" & $iNumber & ")")
MyCheckForError()
If Not @error Then ConsoleWrite("A " & $iIncrementedNumber & @CRLF)

AutoItSetOption("ExpandVarStrings", 0)

$iIncrementedNumber = Execute("_IncrementNumber($iNumber$)")
MyCheckForError()
If Not @error Then ConsoleWrite("B " & $iIncrementedNumber & @CRLF)

AutoItSetOption("ExpandVarStrings", 1)

$iIncrementedNumber = Execute("_IncrementNumber($iNumber$)")
MyCheckForError()
If Not @error Then ConsoleWrite("C " & $iIncrementedNumber & @CRLF)

Func _IncrementNumber($thisnumber)
    Return $thisnumber + 1
EndFunc

Func MyCheckForError($iError = @error, $iExtended = @extended)
    If $ierror Then
        If opt("ExpandVarStrings") = 0 Then
            SetError($ierror, $iextended)
            ConsoleWrite("Execute failed!" & @CRLF)
            ConsoleWrite("error: " & @error & " extended: " & @extended & @CRLF)
            ConsoleWrite("ierror: " & $ierror & " iextended: " & $iextended & @CRLF)
        EndIf
        If opt("ExpandVarStrings") = 1 Then
            SetError($ierror, $iextended)
            ConsoleWrite("Execute failed!@CRLF@")
            ConsoleWrite("error: @error@ extended: @extended@" & @CRLF)
            ConsoleWrite("ierror: $ierror$ extended: @extended@" & @CRLF)
        EndIf
    EndIf
    Return SetError($ierror, $iextended)
EndFunc

 

 

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

×
×
  • Create New...