c.haslam Posted December 24, 2018 Share Posted December 24, 2018 (edited) In my script, an array is created and populated multiple times, and each time (for a run) it is populated with the same number of elements and the same contents. But on a subsequent run, the number of elements may be different. Execution of the script would be faster if the array were populated only once in a run. The obvious way is to create and populate it once, store it as a static the first time in a run, and thereafter check whether it has been populated and if it has, return the value from the function rather than populating again. So how do I do this? Here's my attempt (in outline), which doesn't work. static a10User[0] If Ubound(a10User)=0 then Redim $a10User[50] ; populate it Redim $a10User[$n] Return $a10User Else Return $a10User Endif Edited December 27, 2018 by c.haslam Spoiler CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 24, 2018 Moderators Share Posted December 24, 2018 c.haslam, Something like this seems to work: expandcollapse popup#include <Array.au3> ; Array just to provide different array sizes Global $aBounds[] = [4, 15, 26, 17] For $i = 0 To UBound($aBounds) - 1 ; Call func first time $aRet = _Func() _ArrayDisplay($aRet, "After first call", Default, 8) ; Call func a second time $aRet = _Func() _ArrayDisplay($aRet, "After second call", Default, 8) ;Call func to clear the array $aRet = _Func(1) If UBound($aRet) = 0 Then MsgBox($MB_SYSTEMMODAL, "Array", "Cleared") Else MsgBox($MB_SYSTEMMODAL, "Array", "NOT cleared") EndIf Next Func _Func($iClear = 0) Local Static $aArray[0] If $iClear= 1 Then ReDim $aArray[0] Return EndIf If Ubound($aArray) = 0 Then Redim $aArray[50] ; Populate it For $j = 0 To $aBounds[$i] - 1 $aArray[$j] = $aBounds[$i] Next ReDim $aArray[$aBounds[$i]] EndIf ; You return the array in each case, so just do it once Return $aArray EndFunc You need to clear the array for each "run" as otherwise its Static nature means it will keep the same size. M23 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...
c.haslam Posted December 26, 2018 Author Share Posted December 26, 2018 Melba23: Thank you, but the solution you propose is not the solution to my problem: between runs of my script: $n is unknowable in advance. It is the number of sub-folders under a folder. A user might add a subfolder to the folder between runs of my script. So $n does not change during a run but may change between runs. The cause of my problem is demonstrated by this snippet: Func foo() Static Local $a10[0] If UBound($a10)=0 Then $a10 = [1,2,3] EndIf Return $a10 EndFunc Au3Check says syntax error for the assignment statement. It reports the same error if Static is removed. This: Func foo() Local $a10[0] If UBound($a10)=0 Then ReDim $a10[3] $a10 = [ 1,2,3] EndIf Return $a10 EndFunc gives the same error. I do not consider this to be a bug in AutoIt; rather my last snippet to me is perhaps a desirable feature. Spoiler CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 26, 2018 Moderators Share Posted December 26, 2018 (edited) c.haslam, My code does not seem too far removed from what you appear to want. Here it is amended to have a random size for the array on each run: expandcollapse popup#include <Array.au3> ; Each loop here is a "run" For $i = 0 To 5 ; Call func first time $aRet = _Func() _ArrayDisplay($aRet, "After first call", Default, 8) ; Call func a second time $aRet = _Func() _ArrayDisplay($aRet, "After second call", Default, 8) ;Call func to clear the array $aRet = _Func(1) If UBound($aRet) = 0 Then MsgBox($MB_SYSTEMMODAL, "Array", "Cleared") Else MsgBox($MB_SYSTEMMODAL, "Array", "NOT cleared") EndIf Next Func _Func($iClear = 0) Local Static $aArray[0] ; If the run is ended - clear the array so that the next call will set a new size If $iClear= 1 Then ReDim $aArray[0] Return EndIf If Ubound($aArray) = 0 Then ; Generate a random value to simulate the number of subfolders $iRandom = Random(1, 10, 1) ; Resize the array Redim $aArray[$iRandom] ; And populate it For $j = 0 To UBound($aArray) - 1 $aArray[$j] = $iRandom Next EndIf ; You return the array in each case, so just do it once Return $aArray EndFunc M23 Edited December 26, 2018 by Melba23 Added missing words - interrupted by grandson! 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...
c.haslam Posted December 26, 2018 Author Share Posted December 26, 2018 Thank you! Spoiler CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 26, 2018 Moderators Share Posted December 26, 2018 c.haslam, Glad I could help. M23 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...
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