DarkBoost Posted May 10, 2010 Share Posted May 10, 2010 I often use an array to create variables assigned in a loop (see example below) however using this method I can not set the max Array because this is dynamicly created each time the script is run so I set this to something like 1000 which always works but now I have a massive array. Is there a simply way aruond this? #include <GUIConstantsEx.au3> #include <StaticConstants.au3> Opt("MustDeclareVars", 1) Global $seconds, $random, $position, $width, $lucky Global $array[1000] $seconds = @SEC $random = Random(0, $seconds, 1) $position = 16 $width = $seconds * 16 + 35 If $width <= 250 Then $width = 250 GUICreate("", $width, 50) For $a = 0 To $seconds $array[$a] = GUICtrlCreateLabel($a, 10 + $a * $position, 30, 15, 14, $SS_CENTER) Next $lucky = $array[$random] GUICtrlCreateLabel("Seconds: " & @SEC & @TAB & "|", 10, 10, 85, 14) GUICtrlCreateLabel("Lucky Number: " & $lucky, 110, 10, 90, 14) GUICtrlSetBkColor($array[$lucky], 0xCCCCCC) GUISetState() Do Until GUIGetMsg() = $GUI_EVENT_CLOSE Link to comment Share on other sites More sharing options...
99ojo Posted May 10, 2010 Share Posted May 10, 2010 (edited) Hi, just declare your array after the declaration of $seconds: #include <GUIConstantsEx.au3> #include <StaticConstants.au3> Opt("MustDeclareVars", 1) Global $seconds, $random, $position, $width, $lucky $seconds = @SEC Dim $array [$seconds + 1] ;-)) Stefan @edit: missed the + 1 at array declaration Edited May 10, 2010 by 99ojo Link to comment Share on other sites More sharing options...
hawky358 Posted May 10, 2010 Share Posted May 10, 2010 Like 99ojo says, most of the time you'll know how large your array needs to be, then you can declare it. Sometimes if you add things on the fly you don't know, then you can use _Arrayadd() <- This is much slower if your array gets very large (few thousand) since it basically redims everytime, but for smaller applications it works fine. Here's how you would use it in your program (In this case the array entries will only start at [1] since [0] will be empty (I usually plug the ubound value in there) #include <Array.au3> Global $array[1] _ArrayAdd($array,GUICtrlCreateLabel($a, 10 + $a * $position, 30, 15, 14, $SS_CENTER)) Link to comment Share on other sites More sharing options...
ShawnW Posted May 10, 2010 Share Posted May 10, 2010 Also for dynamic array control look at the ReDim keyword. This allows you to only keep as many as you need. You can write something like... Global $size = 0 Dim $array[10] ; .... Some code ..... ; When you need to add something to $array $size += 1 If $size > UBound($array) Then ReDim $array[$size] ; Add 1 to the size of the array $array[$size-1] = "blah" ; Assigns the new last element in the array to whatever you want. Granted this could cause a time delay on large arrays if you did this on every entry so I like to throttle it and balance memory usage with run time and keep arrays at most 5 or 10 open spaces, and increase as needed by replaceing the ReDim line with If $size > UBound($array) Then ReDim $array[$size+10] ; Add 10 to the size of the array Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 10, 2010 Moderators Share Posted May 10, 2010 (edited) ShawnW,You are correct about ReDim really slowing up the script. If I have to build seriously big arrays in a loop then I use something like this to really reduce the number of ReDim calls: Global $aArray[1] ; Loop ; Increase count $aArray[0] += 1 ; Double size if too small (fewer ReDim needed) If UBound($aArray) <= $aArray[0] Then ReDim $aArray[UBound($aArray) * 2] ; Add element to array $aArray[$aArray[0]] = $variable ; End of loop ; Remove unused elements ReDim $aArray[$aArray[0] + 1]Some people prefer a separate counter so they can have a 0-based array - I prefer to use the [0] element. M23Edit: Typnig! Edited May 10, 2010 by Melba23 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...
hawky358 Posted May 10, 2010 Share Posted May 10, 2010 (edited) Like I said, that's exactly what _Arrayadd() does. Here's the _Arrayadd() function Func _ArrayAdd(ByRef $avArray, $vValue) If Not IsArray($avArray) Then Return SetError(1, 0, -1) If UBound($avArray, 0) <> 1 Then Return SetError(2, 0, -1) Local $iUBound = UBound($avArray) ReDim $avArray[$iUBound + 1] $avArray[$iUBound] = $vValue Return $iUBound EndFunc Anyway, you should get it working now Edited May 10, 2010 by hawky358 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