gpt4 book ai didi

autoit - 我如何构建一个动态数组并在 AutoIt 中循环遍历它?

转载 作者:行者123 更新时间:2023-12-01 22:52:46 32 4
gpt4 key购买 nike

我在看官方AutoIt Array Parameters ,据我所知,你必须告诉 AutoIt 你的数组有多少元素,然后才能创建它。由于我的数组将根据用户在我的界面中选择的内容而动态变化,因此我需要这样的东西。

从他们的页面:

但是假设您事先不知道数组的大小,因为它在动态创建时可能具有可变大小。

Local $iMax

Local $data="Element 1|Element 2|Element 3"

; The string in data will be split into an array everywhere | is encountered
Local $arr = StringSplit($data, "|")

If IsArray($arr) Then
$iMax = UBound($arr); get array size

ConsoleWrite("Items in the array: " & $iMax & @LF)

For $i = 0 to $iMax - 1; subtract 1 from size to prevent an out of bounds error
ConsoleWrite($arr[$i] & @LF)
Next
EndIf

我已经设置了一个 if 语句来检查用户的选择并首先构建数组:

If GUICtrlRead($Box1) = $GUI_CHECKED Then
$data = "one|two|three"
EndIf
If GUICtrlRead($Box2) = $GUI_CHECKED Then
$data = "four|five|six"
EndIf
If GUICtrlRead($Box3) = $GUI_CHECKED Then
$data = "seven|eight|nine"
EndIf

如果用户选择了所有三个框,我需要这样的东西:

$data = one|two|three|four|five|six|seven|eight|nine

然后在这一点上我可以将这些元素传递到上面的示例中以循环遍历我的所有元素。

如何通过多个 if 语句构建数组并得到一个大数组?

最佳答案

您可以使用 UDF 函数 _ ArrayAdd以此目的。例如,

#include <Array.au3>
Local $arr = [] ; NOTE this creates an array of size 1 with an empty string
_ArrayAdd($arr, 'lorem')
_ArrayAdd($arr, 'ipsum')

For $i = 1 To UBound($arr) - 1
ConsoleWrite('arr[' & $i & '] == ' & $arr[$i] & @CRLF)
Next

或者您可以使用 .NET 类 ArrayList 。那更灵活:

Local $arr = ObjCreate('System.Collections.ArrayList')
$arr.Add('lorem')
$arr.Add('ipsum')
$arr.Add('dolor')

ConsoleWrite("Contains 'dolor'? " & $arr.Contains('dolor') & @CRLF)

$arr.Sort()
For $i = 0 To $arr.Count - 1
ConsoleWrite('arr[' & $i & '] == ' & $arr.Item($i) & @CRLF)
Next

关于autoit - 我如何构建一个动态数组并在 AutoIt 中循环遍历它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816816/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com