Quantcast
Channel: AutoIt v3 - General Help and Support
Viewing all articles
Browse latest Browse all 12506

The _ArrayInsert() function fails when element number is too big

$
0
0
I have a test script that creates a 2 element array, then calls _ArrayInsert() to insert data at an element number that's greater than the number of elements in the array.

Here is the test code:
#include <Array.au3>

Local $avArray[2]

$avArray[0] = "Item1"
$avArray[1] = "Item2"

_ArrayDisplay($avArray, "$avArray BEFORE _ArrayInsert()")
_ArrayInsert($avArray, 3, "New")
_ArrayDisplay($avArray, "$avArray AFTER _ArrayInsert()")


The script exits with the following error:
C:\Program Files\AutoIt3\Include\Array.au3 (589) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$avArray[$iElement] = $vValue
^ ERROR

What I would expect is for the function to either return an error or to add the value to the end of the array.
Here is the _ArrayInsert() code from the Array.au3 file in the AutoIT 'include' directroy.
Func _ArrayInsert(ByRef $avArray, $iElement, $vValue = "")
   If Not IsArray($avArray) Then Return SetError(1, 0, 0)
If UBound($avArray, 0) <> 1 Then Return SetError(2, 0, 0)
; Add 1 to the array
Local $iUBound = UBound($avArray) + 1
ReDim $avArray[$iUBound]; Move all entries over til the specified element
For $i = $iUBound - 1 To $iElement + 1 Step -1
$avArray[$i] = $avArray[$i - 1]
Next
; Add the value in the specified element
$avArray[$iElement] = $vValue
Return $iUBound
EndFunc ;==>_ArrayInsert

Viewing all articles
Browse latest Browse all 12506

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>