2014-10-16 81 views
-2

我使用的AutoIt:如何清理此代码以缩短?

$1 = GetItemBySlot(1, 1) 
$2 = GetItemBySlot(1, 2) 
$3 = GetItemBySlot(1, 3) 
$4 = GetItemBySlot(1, 4) 
$5 = GetItemBySlot(1, 5) 

代码重复为40行。我怎样才能缩短它?

+0

[AutoIt增量变量定义]的可能重复(https://stackoverflow.com/questions/7482045/autoit-incremental-variable-definition) – user4157124 2017-10-23 01:44:20

回答

0

您可以通过使用Assign()Eval()缩短此。

For $i = 1 To 5 
    Assign($i, GetItemBySlot(1, $i)) 
Next 

这将是3行,而不是Ñ线。在运行时,这将扩大到:

Assign(1, GetItemBySlot(1, 1)) 
Assign(2, GetItemBySlot(1, 2)) 
Assign(3, GetItemBySlot(1, 3)) 
Assign(4, GetItemBySlot(1, 4)) 
Assign(5, GetItemBySlot(1, 5)) 

为了得到这些,你需要使用Eval函数变量的数据。所以

For $i = 1 To 5 
    Eval($i) 
Next 

返回值GetItemBySlot(1, $i)

0
For $i = 1 To 40 

    $aItemsBySlot[$i] = GetItemBySlot(1, $i) 

Next 

作为每Documentation - Intro - Arrays

数组是包含一系列数据元素的变量。此变量中的每个元素都可以通过索引号访问,该索引号与数组内的元素位置相关 - 在AutoIt中,数组的第一个元素始终为元素[0]。数组元素按照定义的顺序存储并可以排序。

实施例GetItemBySlotMulti()(untestes,没有错误检查):

Global $aItems 

; Assign: 
$aItems = GetItemBySlotMulti(1, 40) 

; Retrieve single value (output item #1 to console): 
ConsoleWrite($aItems[1] & @CRLF) 

; Retrieve all values: 
For $i = 1 To $aItems[0] 

    ConsoleWrite($aItems[$i] & @CRLF) 

Next 

; Retrieve amount of items: 
ConsoleWrite($aItems[0] & @CRLF) 

; Re-assign a single value (re-assign item #1): 
$aItems[1] = GetItemBySlot(1, 1) 

; Function (used to assign example): 
Func GetItemBySlotMulti(Const $iSlot, Const $iItems) 
    Local $aItemsBySlot[$iItems +1] 
     $aItemsBySlot[0] = $iItems 

    For $i = 1 To $iItems 

     $aItemsBySlot[$i] = GetItemBySlot($iSlot, $i) 

    Next 

    Return $aItemsBySlot 
EndFunc 

Related