Keyword Reference


For...In...Next

Enumerates elements in an Object collection or an Array

For <$Variable> In <expression>
    statements
    ...
Next

Parameters

Variable A variable to which an element is being assigned
expression Must be an expression resulting in an Object, or an Array with at least one element

Remarks

The Variable will be created automatically with a Local scope, even when MustDeclareVars is on.
If the expression is an Object collection with no elements, or an multi-dimensional array, the loop will be skipped and the Variable will contain an empty string.
If the expression is not an Object nor an Array, the script stops with an error, unless a COM Error handler had been configured.
AutoIt Arrays are read-only when using For...In. While you can assign the variable inside the For...In loop a value, this change is not reflected in the array itself. To modify the contents of an array during enumeration, use a For...To loop.

For...In...Next statements may be nested.

Related

With...EndWith

Example


;Using an Array
Local $aArray[4]

$aArray[0] = "a"
$aArray[1] = 0
$aArray[2] = 1.3434
$aArray[3] = "test"

Local $string = ""
For $element In $aArray
    $string = $string & $element & @CRLF
Next

MsgBox(0, "For..IN Arraytest", "Result is: " & @CRLF & $string)

;Using an Object Collection

Local $oShell = ObjCreate("shell.application")
Local $oShellWindows = $oShell.windows

If IsObj($oShellWindows) Then
    $string = ""

    For $Window In $oShellWindows
        $string = $string & $Window.LocationName & @CRLF
    Next

    MsgBox(0, "", "You have the following windows open:" & @CRLF & $string)
Else

    MsgBox(0, "", "you have no open shell windows.")
EndIf