Jump to content

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


Recommended Posts

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
Link to comment
Share on other sites

not sure if the error is a bug, but you can use:

_arrayadd

or

_ArrayInsert($avArray, UBound(avArray), "New")

or, you can create your own function:

Func _ArrayInsert2(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)
 
 ; NEW conditions
 If Not IsInt($iElement) Then Return SetError(3, 0, 0)
 If $iElement > UBound($avArray) Then Return SetError(4, 0, 0)
 If $iElement < 0 Then Return SetError(5, 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
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
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...