VeryGut

Include a function as an element in array

6 posts in this topic

#1 ·  Posted

Hello,

I am a begginer when it comes down to scripting and programming. I have not been able to find a thread covering a problem i encountered, nor a working solution.

Is it possible to include a function, as an array element?

I imagine it working like this:

#include <MsgBoxConstants.au3>
#inlude <Array.au3>

Global $Array [5] = ["5", "4", _Function2(), "2", "1" ]

While 1
_Function1()
WEnd

Func _Function1()
For $i=1 to 5
MsgBox ($MB_OK, "", $Array[$i])
Next
EndFunc

Func _Function2()
MsgBox ($MB_OK, "", "Function was executed")
EndFunc

 

Thanks
 

 

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

VeryGut,

Welcome to the AutoIt forums.

You can indeed use a function in an array - like this:

#include <MsgBoxConstants.au3>

HotKeySet("{ESC}", "_Exit")

Global $Array[5] = ["5", "4", _Function2, "2", "1"]

While 1
    _Function1()
WEnd

Func _Function1()
    For $i = 0 To 4
        MsgBox($MB_OK, "", ( (IsFunc($Array[$i])) ? ($Array[$i]()) : ($Array[$i]) ))
    Next
EndFunc   ;==>_Function1

Func _Function2()
    Return "Function was executed"
EndFunc   ;==>_Function2

Func _Exit()
    Exit
EndFunc

You need to check that the array element is a function and then add the trailing () to execute it.

Out of interest, why do you need to do this?

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thanks for the help!

I needed it to automate filling my pdf forms from an excel source file - my script is very primitive (based on ctrl c + ctrl v method with controlclick/controlsend). The problem was that the clipboard was filling up rather quick and I needed to clear it every now and then to keep the script running.

Using an array inside for..next makes it much easier to make a compact script, rather than having to write a line after line of commands in my main function.

Share this post


Link to post
Share on other sites

#5 ·  Posted

VeryGut,

An alternate and more readable technique...

#include <MsgBoxConstants.au3>
#include <Array.au3>

; define 50 element array anf fill with sequential integers
Global $Array[50]
For $1 = 0 To UBound($Array) - 1
    $Array[$1] = $1
Next

_Function1()

Func _Function1()
    For $i = 0 To UBound($Array) - 1
        ConsoleWrite(StringFormat('$i = %02i\tmod value = %02i', $i, Mod($i, 10)) & @CRLF)
        ;every 10th entry run the cleanup routine
        If Mod($i, 10) = 0 And $i <> 0 Then _Function2()
    Next
EndFunc   ;==>_Function1

Func _Function2()
    ConsoleWrite('Ran cleanup' & @CRLF)
EndFunc   ;==>_Function2

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thank you guys for the help. I was unable to get my sript working using the method described in replies. Fortunately i found a workaround, using some of the techniques. Below is my method:

#include <MsgBoxConstants.au3>
#include <Array.au3>

HotKeySet("{ESC}", "_Exit")

Global $Array[5] = ["5", "4", _Function2, "2", "1"]

While 1
    _Function1()
WEnd

Func _Function1 ()
   For $i=1 to 5
      $element = IsFunc($Array[$i])
      If $element = 0 Then
         MsgBox ($MB_OK, "", $Array[$i])
      Else
         Call($Array[$i])
      EndIf
   Next
EndFunc   ;==>_Function1

Func _Function2()
    MsgBox ($MB_OK, "", "Function was executed")
EndFunc

Func _Exit()
    Exit
EndFunc

 

Edited by VeryGut

Share this post


Link to post
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