VeryGut Posted May 15, 2017 Share Posted May 15, 2017 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 AndrewSchultz 1 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 15, 2017 Moderators Share Posted May 15, 2017 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 AndrewSchultz and Skysnake 2 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
VeryGut Posted May 17, 2017 Author Share Posted May 17, 2017 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 More sharing options...
junkew Posted May 17, 2017 Share Posted May 17, 2017 We should start a contest like http://www.ioccc.org/ for AutoIt I agree its powerfull but when used a comment would be nice in your coding for some maintenance understanding in the future. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
kylomas Posted May 18, 2017 Share Posted May 18, 2017 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 AndrewSchultz 1 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 More sharing options...
VeryGut Posted May 18, 2017 Author Share Posted May 18, 2017 (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 May 18, 2017 by VeryGut AndrewSchultz 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now