bob51zhang

Question about passing Arrays

5 posts in this topic

Hey everyone, so before you shoot me with how I didn't do autoIT syntax correctly, I come from a Java background, and I'd love to learn.

I'm having problems passing an Arrays by reference.

Func numStar(Const ByRef $Array)


    For $i = 1 To UBound($Array)
        If $Array[$i] == "*" Then
            $numStar += 1
        EndIf
    Next

;~  MsgBox(0, "JIM LENGTH", UBound($Array))
;~  _ArrayDisplay($arr)

;~  For $i = 1 To UBound($Array)
;~      If (String($Array[$i]) == "*") Then
;~          $numStar += 1
;~      EndIf

;~  Next
    MsgBox(0, "JJ", $numStar)
EndFunc   ;==>numStar

 

Gives me the error

0"F:\Temp\test.au3" (58) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $Array[$i] == "*" Then
If ^ ERROR

$Array: Single character, string array

The function is basically looking for the number of stars in the array.

 

 

Full code:

http://pastebin.com/dDnuc0zM

Share this post


Link to post
Share on other sites



I won't tell you why it's breaking but I will tell you a great way to debug. Use _ArrayDisplay($Array) in your function call and on each iteration in the for loop, print $i to see it's value. You'll figure it out ;)

Also, you can use StringSplit to split a string into an array, each character in an index.

_ArrayDisplay(StringSplit("Example Split Into Characters", "", $STR_NOCOUNT))

 

Share this post


Link to post
Share on other sites

Wild guess... you probably need:

For $i = 1 To UBound($Array) - 1
    If $Array[$i] == "*" Then
        $numStar += 1
    EndIf
Next

Share this post


Link to post
Share on other sites

I'd rather say

For $i = 0 To UBound($Array) - 1

:)

Share this post


Link to post
Share on other sites

Also:

Local $Elements = ["A", "*", "X", "-", "*"]
Local $numStar = 0
For $Element In $Elements
    If $Element == "*" Then $numStar += 1
Next

Saludos

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