Jump to content

Need help on understanding arrays


Recommended Posts

I am not working on a script atm, i just want to understand arrays. Here is my script:

Dim $arr[2][3]

$arr[0][0]="element 1,1"
$arr[0][1]="Element 1,2"
$arr[0][2]="element 1,3"
$arr[1][0]="Element 2,1"
$arr[1][1]="Element 2,2"
$arr[1][2]="element 2,3"

MsgBox(1,"","fds" & $arr[1][0]& $arr[1][1] & $arr[1][2]);I did this just to check whether the array indexes were correct

For $l=1 To 6 Step +1
    For $x=0 to UBound($arr) Step +1
        For $y=0 To UBound($arr, 2) Step +1
            MsgBox(1,"arrays","array contains " & $arr[$x][$y])
            Sleep(50)
        Next
    Next
Next

the message box gives the value of $arr[0][0], $arr[0][1], $arr[0][2], but after that I get the error:

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

MsgBox(1,"arrays","array contains " & $arr[$x][$y])

MsgBox(1,"arrays","array contains " & ^ ERROR

Can someone explain why this is happening?

I just need a small explanation, not a tutorial.

Thanks.

Link to comment
Share on other sites

I understand that part, and I thought I fixed it, but the problem is the same.

where didi make the mistake?

I can't offer an explanation but this works...

Dim $arr[2][3]

$arr[0][0]="element 1,1"
$arr[0][1]="Element 1,2"
$arr[0][2]="element 1,3"
$arr[1][0]="Element 2,1"
$arr[1][1]="Element 2,2"
$arr[1][2]="element 2,3"

MsgBox(1,"","fds" & $arr[1][0]& $arr[1][1] & $arr[1][2]);I did this just to check whether the array indexes were correct

For $x=0 to UBound($arr,1) - 1 
    For $y=0 To UBound($arr, 2) - 1
        MsgBox(1,"arrays","array contains " & $arr[$x][$y])
        Sleep(50)
    Next
Next
Link to comment
Share on other sites

  • Moderators

I am not working on a script atm, i just want to understand arrays. Here is my script:

Dim $arr[2][3]

$arr[0][0]="element 1,1"
$arr[0][1]="Element 1,2"
$arr[0][2]="element 1,3"
$arr[1][0]="Element 2,1"
$arr[1][1]="Element 2,2"
$arr[1][2]="element 2,3"

MsgBox(1,"","fds" & $arr[1][0]& $arr[1][1] & $arr[1][2]);I did this just to check whether the array indexes were correct

For $l=1 To 6 Step +1
    For $x=0 to UBound($arr) Step +1
        For $y=0 To UBound($arr, 2) Step +1
            MsgBox(1,"arrays","array contains " & $arr[$x][$y])
            Sleep(50)
        Next
    Next
Next

the message box gives the value of $arr[0][0], $arr[0][1], $arr[0][2], but after that I get the error:

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

MsgBox(1,"arrays","array contains " & $arr[$x][$y])

MsgBox(1,"arrays","array contains " & ^ ERROR

Can someone explain why this is happening?

I just need a small explanation, not a tutorial.

Thanks.

UBound() contains the upper bound of the index you selected.

If I have

$a_array[2]
Then my UBound = 2

However, the max index range is 1 less than UBound().

So you would have to do:

For $i = 0 To UBound($a_array) - 1
Note the -1.

In addition, Step 1 is standard, there is no need to use Step unless you are indexing the array backwards, then it would be Step -1, or if you are indexing the array in steps of more than one.

Edited by SmOke_N

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

I can't offer an explanation but this works...

Dim $arr[2][3]

$arr[0][0]="element 1,1"
$arr[0][1]="Element 1,2"
$arr[0][2]="element 1,3"
$arr[1][0]="Element 2,1"
$arr[1][1]="Element 2,2"
$arr[1][2]="element 2,3"

MsgBox(1,"","fds" & $arr[1][0]& $arr[1][1] & $arr[1][2]);I did this just to check whether the array indexes were correct

For $x=0 to UBound($arr,1) - 1 
    For $y=0 To UBound($arr, 2) - 1
        MsgBox(1,"arrays","array contains " & $arr[$x][$y])
        Sleep(50)
    Next
Next
Thanks alot for your help.

This code works, though all you changed Step +1 to simply 1.

I don't really understand why though, but I'll figure it out.

thanks again everyone.

Link to comment
Share on other sites

  • Moderators

Thanks alot for your help.

This code works, though all you changed Step +1 to simply 1.

I don't really understand why though, but I'll figure it out.

thanks again everyone.

I explained the "why".

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

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