Keyword Reference


ReDim

Resize an existing array.

ReDim $aArray[subscript 1]...[subscript n]

Parameters

$aArray The name of the array to resize.
subscript The number of elements to create for the array dimension, indexed 0 to n-1.

Remarks

The ReDim keyword is similar to Global/Local, except that ReDim preserves the values in the array instead of removing the values while resizing an array.
The number of dimensions must remain the same, or the old array will be forgotten during the ReDim.
The array will retain the scope (Global or Local) that it had prior to resizing.

Related

Dim, UBound

Example

Example 1

#include <Array.au3>

Example()

Func Example()
        Local $aArray[1] ; Create a 1-dimensional array with 1 element.
        For $i = 0 To 100 ; Loop through values 0 to 100 to access the index of the array
                ReDim $aArray[UBound($aArray) + 1] ; Determine the current size of the array and increase by 1
                $aArray[$i] = $i
        Next
        _ArrayDisplay($aArray) ; Display the array.
EndFunc   ;==>Example

Example 2

#include <MsgBoxConstants.au3>

Example()

Func Example()
        Local $aArray[1] = [0], $iArrayTotal = 5000, $iCount = 0, $iDimension = 0

        Local $hTimer = TimerInit() ; Start the timer.
        For $i = 1 To $iArrayTotal
                ; Returns True if ReDimmed or False if it wasn"t. This uses ByRef, so just pass the Array and a previously declared variable for monitoring the dimension.
                If ReDim1D($aArray, $iDimension) Then
                        $iCount += 1 ; If the array was ReDimmed then add to the ReDim count for output at the end of the example.
                EndIf
                $aArray[0] += 1 ; Increase zeroth index [0] by a value of 1.
                $aArray[$i] = "Row " & $i & ": Col 0" ; Add random data.
        Next
        $hTimer = Round(TimerDiff($hTimer)) ; End the timer and find the difference from start to finish.

        ReDim $aArray[$aArray[0] + 1] ; Remove the empty elements by ReDimming the total count plus the element for holding the count.

        MsgBox($MB_SYSTEMMODAL, "How many times?", "The amount of times a " & $iArrayTotal & " element array was ""ReDimmed"" was " & $iCount & " times." & @CRLF & _
                        "It only took " & $hTimer & " milliseconds to complete.")
        ; If using the method of increasing the array size by 1, $iCount would return 5000, not 29 as demonstrated in this example.
EndFunc   ;==>Example

; ReDim when required by increasing the total array size as totalsize * 1.3 and rounding to the next highest integer.
Func ReDim1D(ByRef $aArray, ByRef $iDimension) ; Where zeroth index [0] is the element count.
        Local $bReturn = False
        If ($aArray[0] + 1) >= $iDimension Then
                $bReturn = True
                $iDimension = Ceiling(($aArray[0] + 1) * 1.3)
                ReDim $aArray[$iDimension]
        EndIf
        Return $bReturn
EndFunc   ;==>ReDim1D