Sign in to follow this  
Followers 0
ds34

static or dynamic "stop" value in FOR loops

4 posts in this topic

I am using for loops to check conditions in arrays, i.e

CODE
For $i = 1 To UBound($CIT_list) - 1

If $CIT_list[$i]=$CIT_list[$i-1] Then _ArrayDelete($CIT_list,$i)

Next

This code occasionally crashes:

: ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

If $TID_list[$i]=$TID_list[$i-1] Then _ArrayDelete($TID_list,$i)

If ^ ERROR

As it appears to me the stop value of the FOR loop is declared upon first run of the loop, i.e. it does not check each run, if the arraysize has changed, thus crashing when removing an entry and changing the arrays dimension? Am I right?

Share this post


Link to post
Share on other sites



correct...

Just add an IF testing the Ubound in your loop and have it do an ExitLoop when it has reached it.

Jos

Two more possibilities:

; Walk array in reverse
For $i = UBound($CIT_list) - 1 To 1 Step -1
    If $CIT_list[$i] = $CIT_list[$i - 1] Then _ArrayDelete($CIT_list, $i)
Next

; Use Do/Until
$i = 1
Do
    If $CIT_list[$i] = $CIT_list[$i - 1] Then _ArrayDelete($CIT_list, $i)
    $i += 1
Until $i >= UBound($CIT_list) - 1

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Thanks for the fast reply.

Should make it to the "for" help.

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  
Followers 0