Jump to content

For...To...Step...Next


Xwolf
 Share

Recommended Posts

I was encountered a problem as following.

#include <Array.au3>

Local $z1[3]

$z1[0] = 1
$z1[1] = 0
$z1[2] = 0

For $i=0 To (UBound($z1)-1) Step 1
    If $z1[$i]=0 Then
        _ArrayDelete($z1,$i)
        $i -= 1
    Else
        ConsoleWrite($z1[$i] & @CRLF)
    EndIf
Next

This is the result.

1

E:\Study\AutoIt3\Scripts\2.au3 (10) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

If $z1[$i]=0 Then

If ^ ERROR

If you can not find solution for this question, please thinking in some minutes before looking down.

I was amazing of the result.

thinking ...

testing ...

I understand it , when i test this code as folowing. >_<

Local $z1=5

For $i=0 To $z1 Step 1
    ConsoleWrite($i & @CRLF)
    $z1 = 2
Next

This is the result.

0

1

2

3

4

5

I looked back to the HELP FILE (For...To...Step...Next ) and find this

"If stepVal or stop is a variable, its value is only read the first time the loop executes."

Oh, my god. :)

Maybe this can help somebody like me. LOL :idiot:

Edited by Xwolf
Link to comment
Share on other sites

  • Moderators

You're decreasing the ubound value of the array every time you delete a part index/element out of it, but you make the To value what it was originally.

#include <Array.au3>

Local $z1[3]

$z1[0] = 1
$z1[1] = 0
$z1[2] = 0

$a_z1_temp = $z1

For $i=0 To (UBound($z1)-1)
    If $z1[$i]=0 Then
        _ArrayDelete($a_z1_temp,$i)
    EndIf
Next
_ArrayDisplay($z1)

$z1 = $a_z1_temp

_ArrayDisplay($z1)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You're decreasing the ubound value of the array every time you delete a part index/element out of it, but you make the To value what it was originally.

#include <Array.au3>

Local $z1[3]

$z1[0] = 1
$z1[1] = 0
$z1[2] = 0

$a_z1_temp = $z1

For $i=0 To (UBound($z1)-1)
    If $z1[$i]=0 Then
        _ArrayDelete($a_z1_temp,$i)
    EndIf
Next
_ArrayDisplay($z1)

$z1 = $a_z1_temp

_ArrayDisplay($z1)
Thanks very much.

Maybe it's useful for somebody else also. :)

Edited by Xwolf
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...