关键字参考


For...In...Next

枚举对象集合或者数组中的元素

For <$Variable> In <表达式>
    语句
    ...
Next

参数

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

注意/说明

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 Array's 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.

相关

With...EndWith

示例/演示


;使用一个数组
Local $aArray[4]

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

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

MsgBox(4096,"For..IN 数组测试","结果: " & @CRLF & $string)

;使用一个对象集合

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(4096,"","您打开了下列窗口:" & @CRLF & $string)
Else

  MsgBox(4096,"","您没有打开外壳窗口.")
EndIf