Jump to content
Sign in to follow this  
Sypher

Arrays, Ubound & Removing entries

Recommended Posts

Sypher

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

Share this post


Link to post
Share on other sites
Jos

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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
SadBunny

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.

Share this post


Link to post
Share on other sites
Sypher

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!

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.