Manhchmal enthält ein Array zusätzliche Einträge, die an einer bestimmten Stelle im Skript nicht benötigt werden. Diese UDF erlaubt es, mit verschiedenen Methoden die gewünschten Einträge aus dem Array herauszufiltern. Die Suche lässt sich dabei auf ein ganzen Element oder einen Teilstring beziehen mit oder ohne Beachtung der Groß-/Kleinschreibung, alternative sind auch regüläre Ausrücke als Muster möglich. Ist das zu durchsuchende Array zweidimensional, ist auch das zu filternde Feld festlegbar.
Funktion: _ArrayFilter
; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayFilter
; Description....: Filters an array for a pattern
; Syntax ........: _ArrayFilter($aArray, $sSearch [, $iMode = 0 [, $iStart = 0 [, $iEnd = -1 [, $iIndex = 0 ]]]])
; Parameters.....: $aArray - [Const ByRef] the array to filter
; $sSearch - the value to search for
; $iMode - Specifies the mode for comparison: (defualt: 0)
; |0 - Case insensitive full match
; |1 - Case sensitive full match
; |2 - Case insensitive substring match
; |3 - Case sensitive substring match
; |4 - regular expression match
; $iStart - the index to begin with (defualt: 0)
; $iEnd - the index to stop filtering (default: -1, Ubound($aArray)-1)
; $iIndex - the subitem index to filter if $aArray is a 2D-Array (default: 0)
; Return values..: Success - Array with the filtered items
; Failure - False and sets @error:
; |1 - $aArray is no array
; |2 - $aArray has too many dimensions (only 1 or 2 allowed)
; |3 - $aArray is 2D-Array and $iIndex is not valid
; |4 - no items were found
; |5 - $iMode is invalid
; Authors........: ProgAndy
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _ArrayFilter(Const ByRef $aArray, $sSearch, $iMode = 0, $iStart = 0, $iEnd = -1, $iIndex = 0)
; ProgAndy
If $iStart < 1 Then $iStart = 0
If IsKeyword($iEnd) Or $iEnd < 0 Or $iEnd >= UBound($aArray) Then $iEnd = UBound($aArray) - 1
Local $fFound = False
Switch UBound($aArray, 0)
Case 0
Return SetError(1, 0, False)
Case 1
Local $j = 0, $aReturn[$iEnd + 1]
For $i = $iStart To $iEnd
Switch $iMode
Case 0
$fFound = $aArray[$i] = $sSearch
Case 1
$fFound = 0 = StringCompare($aArray[$i], $sSearch, 1)
Case 2
$fFound = StringInStr($aArray[$i], $sSearch)
Case 3
$fFound = StringInStr($aArray[$i], $sSearch, 1)
Case 4
$fFound = StringRegExp($aArray[$i], $sSearch)
Case Else
$fFound = False
EndSwitch
If ($fFound) Then
$aReturn[$j] = $aArray[$i]
$j += 1
EndIf
Next
If $j = 0 Then Return SetError(4, 0, False)
ReDim $aReturn[$j]
Case 2
Local $iSub = UBound($aArray, 2) - 1
If $iIndex < 0 Or $iIndex > $iSub Then Return SetError(3, 0, False)
Local $j = 0, $aReturn[$iEnd + 1][$iSub + 1]
For $i = $iStart To $iEnd
Switch $iMode
Case 0
$fFound = $aArray[$i] = $sSearch
Case 1
$fFound = StringCompare($aArray[$i][$iIndex], $sSearch, 1)
Case 2
$fFound = StringInStr($aArray[$i][$iIndex], $sSearch)
Case 3
$fFound = StringInStr($aArray[$i][$iIndex], $sSearch, 1)
Case 4
$fFound = StringRegExp($aArray[$i][$iIndex], $sSearch)
Case Else
$fFound = False
EndSwitch
If ($fFound) Then
For $k = 0 To $iSub
$aReturn[$j][$k] = $aArray[$i][$k]
Next
$j += 1
EndIf
Next
If $j = 0 Then Return SetError(4, 0, False)
ReDim $aReturn[$j][$iSub + 1]
Case Else
Return SetError(2, 0, False)
EndSwitch
Return $aReturn
EndFunc ;==>_ArrayFilter
Changelog
- 19.4.2011: Fehler bei 2D-Arrays korrigiert