Jump to content

Question about passing Arrays


Recommended Posts

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

Link to comment
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))

 

Link to comment
Share on other sites

Also:

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

Saludos

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