Jump to content

Dynamic functions


ptrex
 Share

Recommended Posts

Dynamics Functions

This is an example of a dynamic function.

You can use any AutoIT, Math, Date&Time, String, Variables, Macros, etc, or any combination of it.

Making it possible to change functions interactively with changing the hardcoded data.

And pass that result on to another function if needed.

;
; PTREX dd 06/11/05
; Dynamics Functions
; You can use any AutoIT, Math, Date&Time, String, Variables, Macros, etc, or any combination of it.
; Making it possible to change functions interactively with changing the hardcoded data.
;

#include <GUIConstants.au3>
#include <Date.au3>
#include <String.au3>
#include <Math.au3>
#NoTrayIcon

Dim $Var = 52; Just a number to demonstrate that Vars are precessed correctly.

;Generated with Form Designer preview
$Form1 = GUICreate("Dynamic functions", 622, 441, 192, 113)
$Edit1 = GUICtrlCreateEdit("", 48, 40, 553, 217, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetData($Edit1, "@ComputerName &chr(32) &stringRight(@OSVersion,2) &chr(32) &_Now() &chr(32)& $Var &chr(32) & sqrt(2) &chr(32) &_Calc()/2")
$Label1 = GUICtrlCreateLabel("", 48, 288, 430, 25, $SS_SUNKEN)
$Button1 = GUICtrlCreateButton("Execute", 500, 288, 89, 25)
GUISetState(@SW_SHOW)
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $Button1
        _Dynamic_Functions()
        _Calc()
    EndSelect
WEnd
Exit

Func _Dynamic_Functions()
    $a=GUICtrlRead($Edit1)
$v=Execute($a)  
If @error=1 Then
    Msgbox(0,"Error","There is a syntax Error, Please correct.")
Else
GUICtrlSetData($Label1,$v)
EndIf
EndFunc

Func _Calc()
    $var=$var+1
EndFunc

Thanks to Valik to put me on the right track.

Edited by ptrex
Link to comment
Share on other sites

There seems to be 1 limitation.

Calling a the _Calc (User Defined functions) return a 0 instead of 53.

This means that only External UDF's can be used, or Internal Variables.

This might be by design of the Excecute() function.

Maybe the developers can explain the behaviour ?

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 2 years later...

Hey Ptrex,

Have you ever been able to get a dynamic function to return something?

;testingDynFuncs
#Include <File.au3>
#Include <Array.au3>
Global $DynamicFuncs

If Not _FileReadToArray(@ScriptDir & "\testing.au3", $DynamicFuncs) Then
   MsgBox(4096,"Error", " Error reading the formula ==>>  error:" & @error)
   Exit
EndIf
;_ArrayDisplay($DynamicFuncs)

Local $start
Local $end
For $i = 1 To $DynamicFuncs[0]
    If StringInStr($DynamicFuncs[$i], "Func _CatchAReturn") Then $start = $i+1
    If StringInStr($DynamicFuncs[$i], "EndFunc  ;<===_CatchAReturn") Then $end = $i-1
Next


MsgBox(0, "",_CatchAReturn("25"))
Func _CatchAReturn($number)
    For $j = $start To $end
        Execute($DynamicFuncs[$j])
    Next
EndFunc  ;<===_CatchAReturnoÝ÷ Ù8b²+0«b·lx®­zËb®Ý«­¢+ÙÕ¹}
Ñ¡IÑÕɸ ÀÌØí¹ÕµÈ¤(5Í    ½à À°ÅÕ½ÐìÅÕ½Ðì°ÀÌØí¹ÕµÈ¬Ô¤(5Í   ½à À°ÅÕ½ÐìÅÕ½Ðì°ÀÌØí¹ÕµÈ¬Ð¤)¹Õ¹ì±Ðìôôõ}
Ñ¡IÑÕɸoÝ÷ Ù8^/{
+ËaÇhzÉ赫­¢+ÙÕ¹}
Ñ¡IÑÕɸ ÀÌØí¹ÕµÈ¤(%ÀÌØí¹ÕµÈ±ÐìÌÀQ¡¸IÑÕɸÀÌØí¹ÕµÈ¬ôÔ(%ÀÌØí¹ÕµÈÐìÌÀQ¡¸IÑÕɸÀÌØí¹ÕµÈ´ôÔ)¹Õ¹ì±Ðìôôõ}
Ñ¡IÑÕɸ

Any ideas?

EDIT: In other words I'm trying to execute an IF statement dynamically...

Edited by Oldschool
Link to comment
Share on other sites

Or here is another example:

#Include <File.au3>
#Include <Array.au3>
Global $DynamicFuncs
Global $FuncString
If Not _FileReadToArray(@ScriptDir & "\testing.au3", $DynamicFuncs) Then
   MsgBox(4096,"Error", " Error reading the formula ==>>  error:" & @error)
   Exit
EndIf
;_ArrayDisplay($DynamicFuncs)

Local $start
Local $end
For $i = 1 To $DynamicFuncs[0]
    If StringInStr($DynamicFuncs[$i], "Func _CatchAReturn") Then $start = $i+1
    If StringInStr($DynamicFuncs[$i], "EndFunc  ;<===_CatchAReturn") Then $end = $i-1
Next



MsgBox(0, "",_CatchAReturn("25"))
Func _CatchAReturn($n)
    Local $number = $n
    For $j = $start To $end
        $FuncString = $FuncString & $DynamicFuncs[$j]
    Next
    MsgBox(0, "", $FuncString)
    Execute($FuncString)
    Return $number
EndFunc  ;<===_CatchAReturnoÝ÷ Ø Ý¶­zËb  h¢K%G­+ºÚ"µÍ[ÈÐØ]ÚT]  ÌÍÛ[XBUÚ[HBY    ÌÍÛ[X    ÈÌ[   ÌÍÛ[X
ÏH
BHY ÌÍÛ[X    ÝÈÌ[ ÌÍÛ[XOH
BH^]ÛÜUÑ[[[ÈÉÏOOWÐØ]ÚT]

There is no error when the script is executed, but the $number returned does not change...

Link to comment
Share on other sites

  • Moderators

This is simply a support question, there was no need to bring up a 2+ year old topic.

Edit:

Even I was confused on what you are asking.

Edit2:

Now I don't even know if I was confused :)

$nTest = 4
_ByRefFunc($nTest)
MsgBox(0, 0, $nTest)

Func _ByRefFunc(ByRef $nNum)
    If $nNum <= 3 Then 
        $nNum += 2
    Else
        $nNum -= 2
    EndIf
    Return
EndFunc
Is what I was assuming you were trying to do. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

This is simply a support question, there was no need to bring up a 2+ year old topic.

Edit:

Even I was confused on what you are asking.

Edit2:

Now I don't even know if I was confused :)

Is what I was assuming you were trying to do.

Guy, there is as much logic in my example as there is in a "Hello World!" script. In other words it's simply a proof of concept...

What's not to understand?

[testing]
0=MsgBox(0, "", "Hello World!")

$var = IniRead(@ScriptDir & "\test.ini", "testing", "0", "")
Test()
Func Test()
    Execute($var)
EndFunc
Link to comment
Share on other sites

$return = Execute ("IsNumber ('abc')")
MsgBox (0, "", $return)

Will fail, whereas,

$return = Execute ("IsNumber (10)")
MsgBox (0, "", $return)

works... :)

Edited by Bert
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...