Jump to content

Arrays, Ubound & Removing entries


Sypher
 Share

Recommended Posts

Sorry for the odd title.

I'm running a for-loop with an array. This for loop checks for certain things and removes them if they match.

Example:

For $I = 0 To UBound($TestArray)
   If ($TestArray[$I][1] == "test) Then
     _ArrayDelete2D ($FoundShowArray, $I)
   Endif
Next

When the for-loop starts the array contains 9 entries.

1 / 9: success - not matched - not removed

2 / 9: success - matched - removed

3 / 8: success - matched - removed

4 / 7: success - matched - removed

5 / 6: success - matched - removed

6 / 5: failure: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

I've tried adding a "$items = Ubound($TestArray)" on top, and after the removal $items = $items - 1. This did not work like expected.

I hope someone can help me out, what am i missing here?

Thanks in advance!

Edited by Sypher
Link to comment
Share on other sites

  • Developers

Sorry for the odd title.

I'm running a for-loop with an array. This for loop checks for certain things and removes them if they match.

Example:

For $I = 0 To UBound($TestArray)
   If ($TestArray[$I][1] == "test) Then
     _ArrayDelete2D ($FoundShowArray, $I)
   Endif
Next

When the for-loop starts the array contains 9 entries.

1 / 9: success - not matched - not removed

2 / 9: success - matched - removed

3 / 8: success - matched - removed

4 / 7: success - matched - removed

5 / 6: success - matched - removed

6 / 5: failure: Array variable has incorrect number of subscripts or subscript dimension range exceeded.

I've tried adding a "$items = Ubound($TestArray)" on top, and after the removal $items = $items - 1. This did not work like expected.

I hope someone can help me out, what am i missing here?

Thanks in advance!

you are removing "records" from the array thus the UBOUND is changing. To avoid issues do a reverse scan through the array:

For $I = UBound($TestArray) -1 to 0 Step -1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Or you could use something like a while loop so that the UBound check is performed every loop:

while $items < UBound ($TestArray)
        do stuff with array
        $items += 1
wend

or something else to the same extent, like:

For $I = 0 To UBound($TestArray)
   If ($TestArray[$I][1] == "test) Then
     if $I >= UBound($TestArray) then ExitLoop
     _ArrayDelete2D ($FoundShowArray, $I)
   Endif
Next

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

you are removing "records" from the array thus the UBOUND is changing. To avoid issues do a reverse scan through the array:

For $I = UBound($TestArray) -1 to 0 Step -1
Seems like this one is working :P Damn, i didn't know it was so easy.

Thanks!

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