Jump to content
Sign in to follow this  
Beginner321

Need help on understanding arrays

Recommended Posts

Beginner321

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.

Share this post


Link to post
Share on other sites
trancexx

the index can only be between 0-2 as arrays are Zero Based.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Beginner321

the index can only be between 0-2 as arrays are Zero Based.

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

where didi make the mistake?

Share this post


Link to post
Share on other sites
erik7426

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

Share this post


Link to post
Share on other sites
trancexx

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

where didi make the mistake?

I could just copy/paste that second post from the other thread to answer this.

Just read the help file about UBound() function.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
SmOke_N

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.

Share this post


Link to post
Share on other sites
Beginner321

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.

Share this post


Link to post
Share on other sites
SmOke_N

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.

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.