- Offizieller Beitrag
Arrays sind super Datenspeicher und ich nutze sie häufig als Datenstapel und arbeite die Daten in der Regel nach dem FirstIn-FirstOut Prinzip ab. Dadurch, dass während des Abarbeitens aber weitere Elemente hinzukommen können, ist z.B. _ArrayPop nicht verwendbar.
Um nicht jedes mal das Befüllen/Auslesen(Löschen) auf/vom Stapel neu zu skripten, habe ich für 1D- und 2D-Array diese UDF erstellt:
Code: ArrayStack.au3
_ArrayStack_Create
Erstellt ein Stack-Array (1D/2D) mit Element Zähler an [0]/[0][0] und Typ (FIFO/LIFO) an [1]/[0][1]
_ArrayStack_Add
Fügt Daten an das Ende des Stack hinzu. Für 1D-Array können 1-10 Einzelwerte oder 1 Array mit Werten geschrieben werden. Für 2D-Array können 1-10 'Spalten' oder ein Array mit allen 'Spalten' geschrieben werden.
_ArrayStack_Get
Gibt das erste (FIFO) oder letzte (LIFO) Element des Stapels zurück und löscht es vom Stapel. Einzelwert bei 1D-Array, 1D-Array mit Spaltenwerten bei 2D-Array.
_ArrayStack_Type
Der Typ des Stapels kann während der Nutzung geändert werden.
AutoIt: ArrayStack.au3 v0.1
;-- TIME_STAMP 2018-03-03 11:41:54 v 0.1
Global Const $STACK_FIFO = 1
Global Const $STACK_LIFO = 2
; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayStack_Create
; Description ...: Creates an array (1D/2D) with element counter at [0]/[0][0] and type at [1]/[0][1]
; Syntax ........: _ArrayStack_Create([$_iDim = 1[, $_iDim2Elements = 2]])
; Parameters ....: $_iType - [optional] The stack type (FIFO/LIFO). Default is $STACKFIFO
; $_iDim - [optional] Count of dimensions. Default is 1
; $_iDim2Elements - [optional] Count of elements in 2nd dimension (if exists). Default is 2
; Return values .: The created array with counter = 0 at first arrayposition
; Note ..........: The stack type will stored: 1D-array -> [1] / 2D-array -> [0][1]
; Author ........: BugFix
; ===============================================================================================================================
Func _ArrayStack_Create($_iType=-1, $_iDim=1, $_iDim2Elements=2)
$_iType = ($_iType = -1 Or $_iType = Default) ? $STACK_FIFO : $_iType
Local $aStack[] = [0,$_iType]
If $_iDim = 2 Then
ReDim $aStack[1][$_iDim2Elements]
$aStack[0][0] = 0
$aStack[0][1] = $_iType
EndIf
Return $aStack
EndFunc ;==>_ArrayStack_Create
; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayStack_Add
; Description ...: Adds value(s) at the end of the stack array
; For 1D arrays you can add up to 10 values at once by single param ($_vVal0..$_vVal9) or an 1D array for $_vVal0.
; For 2D arrays you can add only one new entry. Up to 10 values for 2nd dimesion can given by single param ($_vVal0..$_vVal9) or as 1D array for $_vVal0.
; Syntax ........: _ArrayStack_Add(Byref $_aStack, $_vVal0[, $_vVal1 = Null[, $_vVal2 = Null[, $_vVal3 = Null[, $_vVal4 = Null[,
; $_vVal5 = Null[, $_vVal6 = Null[, $_vVal7 = Null[, $_vVal8 = Null[, $_vVal9 = Null]]]]]]]]])
; Parameters ....: $_aStack - The stack array.
; $_vVal0 - The new value or array with values.
; $_vVal1 .. $_vVal9 - [optional] New values (1D) or new values for 2nd dimension (2D).
; Return values .: Success 1
; Failure 0 set @error = 1 given value array ($_vVal0) is not 1D
; 2 none stack array
; Note ..........: New values will every time added from the end, independent of the stack type.
; Author ........: BugFix
; ===============================================================================================================================
Func _ArrayStack_Add(ByRef $_aStack, $_vVal0, $_vVal1=Null, $_vVal2=Null, $_vVal3=Null, $_vVal4=Null, $_vVal5=Null, $_vVal6=Null, $_vVal7=Null, $_vVal8=Null, $_vVal9=Null)
Local $aParam[] = [$_vVal0, $_vVal1, $_vVal2, $_vVal3, $_vVal4, $_vVal5, $_vVal6, $_vVal7, $_vVal8, $_vVal9]
Local $iDim
If Not __ArrayIsStack($_aStack, $iDim) Then Return SetError(2,0,0)
If IsArray($_vVal0) Then
If UBound($_vVal0, 0) > 1 Then Return SetError(1,0,0)
$aParam = $_vVal0
Else
Local $n = 0
For $i = 0 To UBound($aParam) -1
$n += 1
If $aParam[$i] = Null Then
$n -= 1
ExitLoop
EndIf
Next
ReDim $aParam[$n]
EndIf
Switch $iDim
Case 1
Local $idx = $_aStack[0] +2
$_aStack[0] += UBound($aParam)
ReDim $_aStack[$_aStack[0]+2]
For $i = 0 To UBound($aParam) -1
$_aStack[$idx +$i] = $aParam[$i]
Next
Case 2
Local $iUb2 = UBound($_aStack, 2)
$_aStack[0][0] += 1
ReDim $_aStack[$_aStack[0][0]+1][$iUb2]
For $i = 0 To $iUb2 -1
$_aStack[$_aStack[0][0]][$i] = $aParam[$i]
Next
EndSwitch
Return 1
EndFunc ;==>_ArrayStack_Add
; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayStack_Get
; Description ...: Get the first/last item from stack array and delete this
; Syntax ........: _ArrayStack_Get(Byref $_aStack)
; Parameters ....: $_aStack - The stack array.
; Return values .: Success 1D array: The first/last item in array
; 2D array: The first/last item in array as 1D array
; : Failure Null set @error 2 - none stack array
; 1D array: Null set @error 1 - stack array is empty
; : 2D array: [Null] set @error 1 - stack array is empty
; Author ........: BugFix
; ===============================================================================================================================
Func _ArrayStack_Get(ByRef $_aStack)
Local $iDim
If Not __ArrayIsStack($_aStack, $iDim) Then Return SetError(2,0,Null)
Switch $iDim
Case 1
Local $vRet = Null
If $_aStack[0] = 0 Then Return SetError(1,0,$vRet)
If $_aStack[1] = $STACK_FIFO Then
$vRet = $_aStack[2]
If $_aStack[0] > 1 Then
For $i = 2 To $_aStack[0]
$_aStack[$i] = $_aStack[$i+1]
Next
EndIf
Else
$vRet = $_aStack[$_aStack[0]+1]
EndIf
$_aStack[0] -= 1
ReDim $_aStack[$_aStack[0]+2]
Case 2
Local $vRet[] = [Null]
If $_aStack[0][0] = 0 Then Return SetError(1,0,$vRet)
Local $iUb2 = UBound($_aStack, 2)
ReDim $vRet[$iUb2]
If $_aStack[0][1] = $STACK_FIFO Then
For $i = 0 To $iUb2 -1
$vRet[$i] = $_aStack[1][$i]
Next
If $_aStack[0][0] > 1 Then
For $i = 1 To $_aStack[0][0] -1
For $j = 0 To $iUb2 -1
$_aStack[$i][$j] = $_aStack[$i+1][$j]
Next
Next
EndIf
Else
For $i = 0 To $iUb2 -1
$vRet[$i] = $_aStack[$_aStack[0][0]][$i]
Next
EndIf
$_aStack[0][0] -= 1
ReDim $_aStack[$_aStack[0][0]+1][$iUb2]
EndSwitch
Return $vRet
EndFunc ;==>_ArrayStack_Get
; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayStack_Type
; Description ...: Changes the stack type.
; Syntax ........: _ArrayStack_Type(Byref $_aStack[, $_iType = $STACK_FIFO])
; Parameters ....: $_aStack - The stack array.
; $_iType - [optional] The new type. Default is $STACK_FIFO.
; Return values .: Success 1
; Failure 0 set @error = 1 -> None stack array.
; Author ........: BugFix
; ===============================================================================================================================
Func _ArrayStack_Type(ByRef $_aStack, $_iType=$STACK_FIFO)
Local $iDim
If Not __ArrayIsStack($_aStack, $iDim) Then Return SetError(1,0,0)
$_iType = $_iType = $STACK_FIFO ? $STACK_FIFO : $STACK_LIFO
Switch $iDim
Case 1
$_aStack[1] = $_iType
Case 2
$_aStack[0][1] = $_iType
EndSwitch
Return 1
EndFunc ;==>_ArrayStack_Type
Func __ArrayIsStack(ByRef $_aStack, ByRef $_iDim)
If Not IsArray($_aStack) Then Return False
$_iDim = UBound($_aStack, 0)
If $_iDim > 2 Then Return False
Switch $_iDim
Case 1
Return ($_aStack[1] = $STACK_FIFO Or $_aStack[1] = $STACK_LIFO)
Case 2
Return ($_aStack[0][1] = $STACK_FIFO Or $_aStack[0][1] = $STACK_LIFO)
EndSwitch
EndFunc ;==>__ArrayIsStack
Alles anzeigen
AutoIt: ArrayStack_Example.au3
;-- TIME_STAMP 2018-03-03 11:05:18
#include 'ArrayStack.au3'
Global $a1D = _ArrayStack_Create() ; default type FIFO, 1D
Global $a2D = _ArrayStack_Create($STACK_LIFO, 2, 6) ; type LIFO, 2D, 6 elements in 2nd dimension
Global $aVal[] = [31,32,33,34,35,36] ; array with values to add
; add value(s) to 1D array
_ArrayStack_Add($a1D, 3)
_ArrayStack_Add($a1D, 4)
_ArrayStack_Add($a1D, 5)
_ArrayStack_Add($a1D, 6, 7, 8, 9)
_ArrayStack_Add($a1D, $aVal)
; add value(s) to 2D array
_ArrayStack_Add($a2D, 1,2,3,4,5,6)
_ArrayStack_Add($a2D, 11,12,13,14,15,16)
_ArrayStack_Add($a2D, 21,22,23,24,25,26)
_ArrayStack_Add($a2D, $aVal)
; get value by value by used type FIFO
ConsoleWrite('1D Array (FIFO):' & @CRLF)
$x = 0
While 1
$x += 1
If $x = 5 Then ; change the stack type
_ArrayStack_Type($a1D, $STACK_LIFO)
ConsoleWrite('TYPE CHANGED TO LIFO' & @CRLF)
EndIf
$ret = _ArrayStack_Get($a1D)
If @error Then ExitLoop
ConsoleWrite($ret & @CRLF)
WEnd
; get value by value by used type LIFO
ConsoleWrite(@CRLF & '2D Array (LIFO):' & @CRLF)
For $i = 1 To 4
Local $aRet = _ArrayStack_Get($a2D)
Local $s = ''
For $j = 0 To UBound($aRet) -1
$s &= $aRet[$j] & ', '
Next
ConsoleWrite(StringTrimRight($s, 2) & @CRLF)
Next
Alles anzeigen