Jump to content

ReDim - When?


Valik
 Share

Recommended Posts

Sounds to me like the _ArrayRedim() function can be removed from the upcoming revision to the standard library, correct?

;===============================================================================
; 
; Function Name:  _ArrayRedim()
; Description:    Resizes the given array to the specified new size.
; Author(s):      David Nuttall <danuttall@rocketmail.com>
; 
;===============================================================================
Func _ArrayRedim(ByRef $avArray, $iNewSize)
   ; Check parameter validity.
    Select
        Case (Not IsArray($avArray))
            SetError(1)
            Return 0
        Case (UBound($avArray, 0) > 1)
            SetError(2)
            Return 0
        Case (Not StringIsInt($iNewSize))
            SetError(3)
            Return 0
        Case ($iNewSize < 1)
            SetError(4)
            Return 0
    EndSelect
    
   ; Declare variables.
    Local $aTmp = $avArray, $iCntr = 0
    Dim $avArray[$iNewSize]
    
    For $iCntr = 0 To _Min(UBound($aTmp), $iNewSize) - 1
        $avArray[$iCntr] = $aTmp[$iCntr]
    Next

    For $iCntr = $iCntr To UBound($avArray) - 1
        $avArray[$iCntr] = ""
    Next 
    
    SetError(0)
    Return 1
EndFunc  ;==> _ArrayRedim()

How about _ArrayAdd(), _ArrayInsert(), etc.?

Sincerely yours,Jeremy Landesjlandes@landeserve.com

Link to comment
Share on other sites

If your libraries are going along with the version 3.0.102 stable (whenever that comes), then yes the ArrayReDim() is not needed. As for the others:

Func _ArrayInsert(ByRef $array)
   ReDim $Array[UBound($array)+1]
EndFunc

Func _ArrayAdd
   Rem What does this do again?
EndFunc

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Here's what I have for the array functions, please let me know what needs changed:

#include-once

; ------------------------------------------------------------------------------
;
; AutoIt Version: 3.0
; Language:       English
; Description:    Functions that assist with array management.
;
; ------------------------------------------------------------------------------


#include <Math.au3>


;===============================================================================
;
; Function Name:  _ArrayAdd()
; Description:    Adds a specified value at the end of an array, returning the
;                 adjusted array.
; Author(s):      Cephas <cephas@clergy.net>
; 
;===============================================================================
Func _ArrayAdd($avArray, $sValue)
    Local $avNewArray = $avArray
    Local $iUpper
    
    If IsArray($avArray) Then
        $iUpper = UBound($avNewArray)
        $avNewArray = _ArrayInsert($avNewArray, $iUpper)
        $avNewArray[$iUpper] = $sValue
        SetError(0)
    Else
        Local $avNewArray[1]
        $avNewArray[0] = $sValue
        SetError(1)
    EndIf
    Return $avNewArray
EndFunc  ;==> _ArrayAdd()


;===============================================================================
; 
; Function Name:  _ArrayBinarySearch()
; Description:    Uses the binary search algorithm to search through a
;                 1-dimensional array.
; Author(s):      Jos van der Zande <jvdzande@yahoo.com>
; 
;===============================================================================
Func _ArrayBinarySearch(ByRef $avArray, ByRef $iKey)
    Local $iLwrLimit = 1
    
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return ""
    EndIf
    
    $iUprLimit = UBound($avArray) - 1
    $iMidElement = Int(($iUprLimit + $iLwrLimit) / 2)
    
    If $avArray[$iLwrLimit] > $iKey Or $avArray[$iUprLimit] < $iKey Then
        SetError(2)
        Return ""
    EndIf
    
    While $iLwrLimit <= $iMidElement And $iKey <> $avArray[$iMidElement]
        If $iKey < $avArray[$iMidElement] Then
            $iUprLimit = $iMidElement - 1
        Else
            $iLwrLimit = $iMidElement + 1
        EndIf
        $iMidElement = Int(($iUprLimit + $iLwrLimit) / 2)
    Wend
    
    If $iLwrLimit > $iUprLimit Then
        SetError(3)
        Return ""
    Else
        SetError(0)
        Return $iMidElement
    EndIf
EndFunc  ;==> _ArrayBinarySearch()


;===============================================================================
; 
; Function Name:  _ArrayDelete()
; Description:    Deletes the specified element from the given array, returning
;                 the adjusted array.
; Author(s)       Cephas <cephas@clergy.net>
; 
;===============================================================================
Func _ArrayDelete($avArray, $iElement)
    Local $iCntr = 0, $iUpper = 0, $iNewSize = 0
    
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return ""
    EndIf
    
   ; We have to define this here so that we're sure that $avArray is an array
   ; before we get it's size.
    Local $iUpper = UBound($avArray)   ; Size of original array
    
   ; If the array is only 1 element in size then we can't delete the 1 element.
    If $iUpper = 1 Then
        SetError(2)
        Return ""
    EndIf
    
    Local $avNewArray[$iUpper - 1]
    If $iElement < 0 Then
        $iElement = 0
    EndIf
    If $iElement > ($iUpper - 1) Then
        $iElement = ($iUpper - 1)
    EndIf
    If $iElement > 0 Then
        For $iCntr = 0 To $iElement - 1
            $avNewArray[$iCntr] = $avArray[$iCntr]
        Next
    EndIf
    If $iElement < ($iUpper - 1) Then
        For $iCntr = ($iElement + 1) To ($iUpper - 1)
            $avNewArray[$iCntr - 1] = $avArray[$iCntr]
        Next
    EndIf
    
    SetError(0)
    Return $avNewArray
EndFunc  ;==> _ArrayDelete()


;===============================================================================
; 
; Function Name:  _ArrayDisplay()
; Description:    Displays a 1-dimensional array in a message box.
; Author(s):      Brian Keene <brian_keene@yahoo.com>
; 
;===============================================================================
Func _ArrayDisplay(ByRef $avArray, $sTitle)
    Local $iCounter = 0, $sMsg = ""
    
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return 0
    EndIf
    
    For $iCounter = 0 To UBound($avArray) - 1
        $sMsg = $sMsg & "[" & $iCounter & "]    = " & StringStripCR($avArray[$iCounter]) & @CR
    Next
    
    MsgBox(4096, "Display Array - " & $sTitle, $sMsg)
    SetError(0)
    Return 1
EndFunc  ;==> _ArrayDisplay()


;===============================================================================
; 
; Function Name:  _ArrayInsert()
; Description:    Inserts a blank element into an array, returning the adjusted
;                 array.
; Author(s):      Cephas <cephas@clergy.net>
;
;===============================================================================
Func _ArrayInsert($avArray, $iElement)
    Local $iCntr = 0
    
    If (Not IsArray($avArray)) Then
        Local $avNewArray[1] = $avArray
        SetError(1)
        Return $avNewArray
    EndIf
    
    Local $iUpper = UBound($avArray)
    Local $avNewArray[$iUpper + 1]
    
    If ($iElement > $iUpper) Then
        $iElement = $iUpper
    EndIf
    If ($iElement < 0) Then
        $iElement = 0
    EndIf
    If ($iElement > 0) Then
        For $iCntr = 0 To ($iElement - 1)
            $avNewArray[$iCntr] = $avArray[$iCntr]
        Next
    EndIf
    If ($iElement < $iUpper) Then
        For $iCntr = ($iElement + 1) To UBound($avNewArray) - 1
            $avNewArray[$iCntr] = $avArray[$iCntr - 1]
        Next
    EndIf
    SetError(0)
    Return $avNewArray
EndFunc  ;==> _ArrayInsert()


;===============================================================================
; 
; Function Name:  _ArrayMax()
; Description:    Returns the highest value held in an array.
; Author(s):      Cephas <cephas@clergy.net>
;
;===============================================================================
Func _ArrayMax($avArray)
    If IsArray($avArray) Then
        Return $avArray[_ArrayMaxIndex($avArray)]
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc  ;==> _ArrayMax()


;===============================================================================
; 
; Function Name:  _ArrayMaxIndex()
; Description:    Returns the index where the highest value occurs in the array.
; Author(s):      Cephas <cephas@clergy.net>
; 
;===============================================================================
Func _ArrayMaxIndex($avArray)
    Local $iCntr, $iMaxIndex = 0
    
    If Not IsArray($avArray) Then
        SetError(1)
        Return ""
    EndIf
    
    Local $iUpper = UBound($avArray)
    For $iCntr = 1 To ($iUpper - 1)
        If $avArray[$iMaxIndex] < $avArray[$iCntr] Then
            $iMaxIndex = $iCntr
        EndIf
    Next
    SetError(0)
    Return $iMaxIndex
EndFunc  ;==> _ArrayMaxIndex()


;===============================================================================
;
; Function Name:  _ArrayMin()
; Description:    Returns the lowest value held in an array.
; Author(s):      Cephas <cephas@clergy.net>
; 
;===============================================================================
Func _ArrayMin($avArray)
    If IsArray($avArray) Then
        Return $avArray[_ArrayMinIndex($avArray)]
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc  ;==> _ArrayMin()


;===============================================================================
; 
; Function Name:  _ArrayMinIndex()
; Description:    Returns the index where the lowest value occurs in the array.
; Author(s):      Cephas <cephas@clergy.net>
; 
;===============================================================================
Func _ArrayMinIndex($avArray)
    Local $iCntr = 0, $iMinIndex = 0
    
    If Not IsArray($avArray) Then
        SetError(1)
        Return ""
    EndIf
    
    Local $iUpper = UBound($avArray)
    For $iCntr = 1 To ($iUpper - 1)
        If $avArray[$iMinIndex] > $avArray[$iCntr] Then
            $iMinIndex = $iCntr
        EndIf
    Next
    SetError(0)
    Return $iMinIndex
EndFunc  ;==> _ArrayMinIndex()


;===============================================================================
; 
; Function Name:  _ArrayPop()
; Description:    Returns the last element of an array, deleting that element
;                 from the array at the same time.
; Author(s):      Cephas <cephas@clergy.net>
; 
;===============================================================================
Func _ArrayPop(ByRef $avArray)
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return ""
    EndIf
    
   ; Save the value in the last element of the array.
    Local $sLastVal = $avArray[UBound($avArray) - 1]
    
   ; Resize the size of the array down by the last element.
    $avArray = _ArrayDelete($avArray, UBound($avArray) - 1)
    
   ; Return the value we saved from the last element of $avArray.
    SetError(0)
    Return $sLastVal
EndFunc  ;==> _ArrayPop()


;===============================================================================
; 
; Function Name:  _ArrayPush()
; Description:    Adds an element to the end of an array, expanding the array
;                 to accommodate the extra element.
; Author(s):      Cephas <cephas@clergy.net>
; 
;===============================================================================
Func _ArrayPush($avArray, $sNewValue)
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return ""
    EndIf
    
    $avArray = _ArrayAdd($avArray, $sNewValue)
    SetError(0)
    Return $avArray
EndFunc  ;==> _ArrayPush()


;===============================================================================
; 
; Function Name:  _ArrayRedim()
; Description:    Resizes the given array to the specified new size.
; Author(s):      David Nuttall <danuttall@rocketmail.com>
; 
;===============================================================================
Func _ArrayRedim(ByRef $avArray, $iNewSize)
   ; Check parameter validity.
    Select
        Case (Not IsArray($avArray))
            SetError(1)
            Return 0
        Case (UBound($avArray, 0) > 1)
            SetError(2)
            Return 0
        Case (Not StringIsInt($iNewSize))
            SetError(3)
            Return 0
        Case ($iNewSize < 1)
            SetError(4)
            Return 0
    EndSelect
    
   ; Declare variables.
    Local $aTmp = $avArray, $iCntr = 0
    Dim $avArray[$iNewSize]
    
    For $iCntr = 0 To _Min(UBound($aTmp), $iNewSize) - 1
        $avArray[$iCntr] = $aTmp[$iCntr]
    Next

    For $iCntr = $iCntr To UBound($avArray) - 1
        $avArray[$iCntr] = ""
    Next 
    
    SetError(0)
    Return 1
EndFunc  ;==> _ArrayRedim()


;===============================================================================
; 
; Function Name:  _ArrayReverse()
; Description:    Takes the given array and reverses the order in which the
;                 elements appear in the array.
; Author(s):      Brian Keene <brian_keene@yahoo.com>
; 
;===============================================================================
Func _ArrayReverse(ByRef $avArray)
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return ""
    EndIf
    
   ; Create a copy of the array.
    Local $avNewArray = $avArray
    
   ; Declare and define variables based on the array.
    Local $iIndex1, $iIndex2 = UBound($avNewArray) - 1
    Local $iMidPoint = $iIndex2 / 2
    
   ; If the array has an odd # of elements then find a slight midpoint.
    If (Not Int($iMidPoint)) Then
        $iMidPoint = Round($iMidPoint, 0)
    EndIf
    
   ; Reverse the elements in the array.
    For $iIndex1 = 0 To UBound($avNewArray) - 1 Step 1
        If ($iIndex1 <= $iMidPoint) Then
            _ArraySwap($avNewArray[$iIndex1], $avNewArray[$iIndex2])
        EndIf
        $iIndex2 = $iIndex2 - 1
    Next
    
    SetError(0)
    Return $avNewArray
EndFunc  ;==> _ArrayReverse()


;===============================================================================
; 
; Function Name:  _ArraySwap()
; Description:    Swaps two elements of an array.
; Author(s):      David Nuttall <danuttall@rocketmail.com>
; 
;===============================================================================
Func _ArraySwap(ByRef $svector1, ByRef $svector2)
    Local $sTemp = $svector1
    
    $svector1 = $svector2
    $svector2 = $sTemp
    
    SetError(0)
EndFunc  ;==> _ArraySwap()


;===============================================================================
; 
; Function Name:  _ArrayToClip()
; Description:    Sends the contents of an array to the clipboard.
; Author(s):      Cephas <cephas@clergy.net>
;
;===============================================================================
Func _ArrayToClip($avArray)
    Local $iCntr, $iRetVal = 0, $sCr = "", $sText = ""
    
    If (IsArray($avArray)) Then
        For $iCntr = 0 To (UBound($avArray) - 1)
            $iRetVal = 1
            If $iCntr > 0 Then
                $sCr = @CR
            EndIf
            $sText = $sText & $sCr & $avArray[$iCntr]
        Next
    EndIf
    ClipPut($sText)
    Return $iRetVal
EndFunc  ;==> _ArrayToClip()


;===============================================================================
; 
; Function Name:  _ArrayToString()
; Description:    Places the elements of an array into a single string,
;                 separated by the specified delimiter.
; Author(s):      Brian Keene <brian_keene@yahoo.com>
; 
;===============================================================================
Func _ArrayToString(ByRef $avArray, $iStart, $iEnd, $sDelim)
   ; Declare local variables.
    Local $iCntr = 0, $iUBound = 0, $sResult = ""

   ; If $avArray is an array then set var for efficiency sake.
    If (IsArray($avArray)) Then
        $iUBound = UBound($avArray) - 1
    EndIf
    
   ; Check for parameter validity.
    Select
        Case (Not IsArray($avArray))
            SetError(1)
            Return ""
        Case( ($iUBound + 1) < 2 Or UBound($avArray, 0) > 1)
            SetError(2)
            Return ""
        Case (Not IsInt($iStart))
            SetError(3)
            Return ""
        Case (Not IsInt($iEnd))
            SetError(5)
            Return ""
        Case (Not IsString($sDelim))
            SetError(7)
            Return ""
        Case ($sDelim = "")
            SetError(8)
            Return ""
        Case (StringLen($sDelim) > 1)
            SetError(9)
            Return ""
        Case ($iStart = -1 And $iEnd = -1)
            $iStart = 0
            $iEnd = $iUBound
        Case ($iStart < 0)
            SetError(4)
            Return ""
        Case ($iEnd < 0)
            SetError(6)
            Return ""
    EndSelect
    
   ; Make sure that $iEnd <= to the size of the array.
    If ($iEnd > $iUBound) Then
        $iEnd = $iUBound
    EndIf

   ; Combine the elements into the string.
    For $iCntr = $iStart To $iEnd
        $sResult = $sResult & $avArray[$iCntr]
        If ($iCntr < $iEnd) Then
            $sResult = $sResult & $sDelim
        EndIf
    Next
    
    SetError(0)
    Return $sResult
EndFunc  ;==> _ArrayToString()

Sincerely yours,Jeremy Landesjlandes@landeserve.com

Link to comment
Share on other sites

Here's what I have for the array functions, please let me know what needs changed:

Ok, here goes. Oh, you will want to add error checking like, $Array is an array and there are elements in the array already, fix my spelling errors, etc.

#include-once

; ------------------------------------------------------------------------------
;
; AutoIt Version: 3.0
; Language:       English
; Description:    Functions that assist with array management.
;
; ------------------------------------------------------------------------------


#include <Math.au3>


;===============================================================================
;
; Function Name:  _ArrayAdd()
; Description:    Adds a specified value at the end of an array, returning the
;                 adjusted array.
; Author(s):      Cephas <cephas@clergy.net>
;
;===============================================================================
Func _ArrayAdd($avArray, $sValue)
   Local $avNewArray = $avArray
   Local $iUpper
   
   If IsArray($avArray) Then
       $iUpper = UBound($avNewArray)
       ReDim $Array[$iUpper+1]
       $avNewArray[$iUpper] = $sValue
       SetError(0)
   Else
       Local $avNewArray[1]
       $avNewArray[0] = $sValue
       SetError(1)
   EndIf
   Return $avNewArray
EndFunc;==> _ArrayAdd()

;===============================================================================
;
; Function Name:  _ArrayDelete()
; Description:    Deletes the specified element from the given array, returning
;                 the adjusted array.
; Author(s)       Cephas <cephas@clergy.net>
;
;===============================================================================
Func _ArrayDelete($avArray, $iElement)
   Local $iCntr = 0, $iUpper = 0, $iNewSize = 0
   
   If (Not IsArray($avArray)) Then
       SetError(1)
       Return ""
   EndIf
   
; We have to define this here so that we're sure that $avArray is an array
; before we get it's size.
   Local $iUpper = UBound($avArray) ; Size of original array
   
; If the array is only 1 element in size then we can't delete the 1 element.
   If $iUpper = 1 Then
       SetError(2)
       Return ""
   EndIf
   
   ReDim $avArray[$iUpper - 1]
   If $iElement < 0 Then
       $iElement = 0
   EndIf
   If $iElement > ($iUpper - 1) Then
       $iElement = ($iUpper - 1)
   EndIf
   If $iElement < ($iUpper - 1) Then
       For $iCntr = $iElement  To ($iUpper - 2)
           $avArray[$iCntr] = $avArray[$iCntr+1]
       Next
   EndIf
   
   Return $avArray
EndFunc;==> _ArrayDelete()


Func _ArrayInsert(ByRef $Array, $location)
    If IsArray($Array) Then
        Local $i
        ReDim $Array[UBound($Array)+1]

        If $Location < UBound($Array) then ;If not, do not copy
           For $i = UBound($Array)-1 to $Location Step -1
               $Array[$i] = $Array[$i-1]
           Next
        EndIf
    EndIf
EndFunc

;===============================================================================
;
; Function Name:  _ArrayPop()
; Description:    Returns the last element of an array, deleting that element
;                 from the array at the same time.
; Author(s):      Cephas <cephas@clergy.net>
; Screwed around with by David Nuttall
;
;===============================================================================
Func _ArrayPop(ByRef $avArray)
   If (Not IsArray($avArray)) Then
       SetError(1)
       Return ""
   EndIf
   
; Save the value in the last element of the array.
   Local $sLastVal = $avArray[UBound($avArray) - 1]
   
; Resize the size of the array down by the last element.
   ReDim $avArray[UBound($avArray) - 1]
   
; Return the value we saved from the last element of $avArray.
   SetError(0)
   Return $sLastVal
EndFunc;==> _ArrayPop()

;===============================================================================
;
; Function Name:  _ArraySwap()
; Description:    Swaps two variables or elements of an array.
; Author(s):      David Nuttall <danuttall@rocketmail.com>
;
;===============================================================================

_ArrayBinarySearch, _ArrayDisplay, _ArrayMax, _ArrayMin, _ArrayMaxIndex, _ArrayMinIndex, _ArrayReverse, _ArrayToClip, _ArrayToString are unaffected.

_ArrayPush calls a routine that should be adjusted as given above.

_ArrayReDim is removed and replaced with ReDim keyword.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Do you mean _ArrayPush() or _ArrayPop() needs modified? You included _ArrayPop() in the code section, but mentioned _ArrayPush() in your description at the bottom of your post.

Sincerely yours,Jeremy Landesjlandes@landeserve.com

Link to comment
Share on other sites

_ArrayPop would be more optimal to call ReDim directly, but _ArrayPush could use the rewritten _ArrayAdd. Actually, a version of _ArrayPush that calls ReDim directly would be a little more optimal and not that much larger.

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Jon, why can we not use ReDim in a single-line If statement? I get this error:

This keyword cannot be used after a "Then" keyword.:

If $i > 0 Then ReDim $array[$i+1]If $i > 0 Then ^ ERROR

However, this works:

If $i > 0 Then
    ReDim $array[$i+1]
EndIf
Edited by Valik
Link to comment
Share on other sites

  • Administrators

Ah, I added some checking code ages ago to stop certain commands being using in a single line IF. I think I coded it by commands that were allowed to be used so I'll have to add this keyword too.

Ta.

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