Jump to content

setting array variables to dynamic functions


Recommended Posts

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

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

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

  • Moderators

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: :idea:

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

M23

Edit: Typnig!

Edited by Melba23

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

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 :idea:

Edited by hawky358
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...