Jump to content

Include a function as an element in array


Recommended Posts

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
 

 

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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