Jump to content
Sign in to follow this  
BrettF

Array Out Of Bounds

Recommended Posts

BrettF

Now this is strange behaviour. I noticed it today while experimenting with GeoSoft. It appears, when you delete an element using _ArrayDelete, it still manages to complete the whole array.

Example 1:

#include <Array.au3>

Dim $avArray[10]
$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

$orr = UBound ($avArray) -1
For $i = 0 To UBound ($avArray) -1
    ConsoleWrite ($i & "/" & $orr & " out of " & UBound ($avArray) - 1 & " possible elements" & @CRLF)
    If $i = 0 Then
        ConsoleWrite ("Element 8 (" & $avArray[8] & ") was deleted" & @CRLF)
        _ArrayDelete( $avArray,8)
    EndIf
Next

Now the next interesting bit is when a value is hard coded, it is able to exceed the array element total. Example:

#include <Array.au3>

Dim $avArray[10]
$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

$orr = UBound ($avArray) -1
For $i = 0 To 15
    ConsoleWrite ($i & "/" & $orr & " out of " & UBound ($avArray) - 1 & " possible elements" & @CRLF)
    If $i = 0 Then
        ConsoleWrite ("Element 8 (" & $avArray[8] & ") was deleted" & @CRLF)
        _ArrayDelete( $avArray,8)
    EndIf
Next

That definately should have failed...

Just wondered if this was a bug, or if it was intentional.

Cheers

Brett

EDIT: XP SP2, AutoIt V3.2.10.0

Edited by Bert

Share this post


Link to post
Share on other sites
BrettF
BrettF

Of course. I'm not thinking straight. Bit of a lack of sleep. The array was never accessed, so it had no reason to fail. Duh! Don't mind me folks...

EDIT: But why does $i still exceed the Ubound in the first example?

EDIT 2:

If I do this (My code before was all wrong. WTF was with my Ubound () -1... mmmmmm... :D):

#include <Array.au3>

Dim $avArray[10]
$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

$orr = UBound ($avArray)
For $i = 0 To UBound ($avArray)
    ConsoleWrite ($i & "/" & $orr & " out of " & UBound ($avArray) & " possible elements [" & $avArray[$i] & "]" & @CRLF)
    If $i = 0 Then
        ConsoleWrite ("Element 8 (" & $avArray[8] & ") was deleted" & @CRLF)
        _ArrayDelete( $avArray,8)
    EndIf
Next

It fails. Mainly from what I can see was because when you deleted the element, then Ubound wasn't updated.

Is that because the value of UBound is recorded by AutoIt, then is used from there in the loop?

Edited by Bert

Share this post


Link to post
Share on other sites
randallc

Of course. I'm not thinking straight. Bit of a lack of sleep. The array was never accessed, so it had no reason to fail. Duh! Don't mind me folks...

EDIT: But why does $i still exceed the Ubound in the first example?

EDIT 2:

If I do this (My code before was all wrong. WTF was with my Ubound () -1... mmmmmm... :D):

#include <Array.au3>

Dim $avArray[10]
$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray[5] = "Valik"
$avArray[6] = "Cyberslug"
$avArray[7] = "Nutster"
$avArray[8] = "JdeB"
$avArray[9] = "Tylo"

$orr = UBound ($avArray)
For $i = 0 To UBound ($avArray)
    ConsoleWrite ($i & "/" & $orr & " out of " & UBound ($avArray) & " possible elements [" & $avArray[$i] & "]" & @CRLF)
    If $i = 0 Then
        ConsoleWrite ("Element 8 (" & $avArray[8] & ") was deleted" & @CRLF)
        _ArrayDelete( $avArray,8)
    EndIf
Next

It fails. Mainly from what I can see was because when you deleted the element, then Ubound wasn't updated.

Is that because the value of UBound is recorded by AutoIt, then is used from there in the loop?

Yes, I think.

You have to start at ubound-1 and go step -1 if you are going to use arraydelete in a loop...

Best, Randall

Share this post


Link to post
Share on other sites
BrettF

Yes, I think.

You have to start at ubound-1 and go step -1 if you are going to use arraydelete in a loop...

Best, Randall

Ok. Thanks. :D I started to confuse myself... :P

Share this post


Link to post
Share on other sites
Valik

The "Stop" value is calculated once and stored. It is never re-evaluated.

Share this post


Link to post
Share on other sites
BrettF

Share this post


Link to post
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
Sign in to follow this  

×