Jump to content

static or dynamic "stop" value in FOR loops


ds34
 Share

Recommended Posts

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?

Link to comment
Share on other sites

  • Developers

correct...

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

Jos

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

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