2011-02-03 47 views
0

我如何使用另一种阵列VB6滤波器使用另一种阵列VB6

编辑 过滤阵列给定的阵列A,除去在从数组A

+2

,给予两个数组“A”和“B”从`A`获取元素数组,这个元素也存在于`B`中? – 2011-02-03 18:59:26

+0

没有我添加更多信息 – Smith 2011-02-03 19:19:23

回答

0

我已经找到自己的答案,感谢所有谁作为贡献

Function FilterArray(ByVal Source As String, ByVal Search As String, Optional _ 
    ByVal Keep As Boolean = True) As String 


    Dim i As Long 
    Dim SearchArray()  As String 
    Dim iSearchLower  As Long 
    Dim iSearchUpper  As Long 

    If LenB(Source) <> 0 And LenB(Search) <> 0 Then 
     SearchArray = Split(Search, " ") 
    Else 
     FilterArray = Source 
     Exit Function 
    End If 
    iSearchLower = LBound(SearchArray) 
    iSearchUpper = UBound(SearchArray) 

    For i = iSearchLower To iSearchUpper 
     DoEvents 
     Source = Join(Filter(Split(Source, " "), SearchArray(i), Keep, _ 
      vbTextCompare), " ") 
    Next i 
    FilterArray = Source 
End Function 
0

数组B中的所有元件。在这种情况下,I”的阵列d只是对一个数组进行排序,然后遍历第二个数组,如果发现它们,则从第一个数组中删除它们。这个算法似乎需要O(n lg n)并且做你想做的事情。

0

假设他们为整型数组:

Dim FilteredArray() As Integer 
Dim X as Long 
Dim Y as Long 
Dim Z as Long 
Dim bDupe as Boolean 

Z = -1 
For X = 0 to UBound(A) 
    bDupe = False 
    For Y = 0 to UBound(B) 
      If A(X) = B(Y) Then 
       bDupe = True 
       Exit For 
      End If 
    Next 
    If Not bDupe Then 
      Z = Z + 1 
      ReDim Preserve FilteredArray(Z) 
      FilteredArray(Z) = A(X) 
    End If 
Next 
+0

数组elemenets是字符串 – Smith 2011-02-04 08:15:22

0

尝试是这样的

Option Explicit 

Private Sub Form_Load() 
    Dim vElem   As Variant 

    For Each vElem In SubstractArray(Array("aa", "b", "test"), Array("c", "aa", "test")) 
     Debug.Print vElem 
    Next 
End Sub 

Private Function SubstractArray(arrSrc As Variant, arrBy As Variant) As Variant 
    Dim cIndex   As Collection 
    Dim vElem   As Variant 
    Dim vRetVal   As Variant 
    Dim lIdx   As Long 

    If UBound(arrSrc) < LBound(arrSrc) Then 
     Exit Function 
    End If 
    '--- build index collection 
    Set cIndex = New Collection 
    For Each vElem In arrBy 
     cIndex.Add vElem, "#" & vElem 
    Next 
    '--- allocate output array 
    lIdx = LBound(arrSrc) 
    ReDim vRetVal(lIdx To UBound(arrSrc)) As Variant 
    '--- iterate source and seek in index 
    For Each vElem In arrSrc 
     On Error Resume Next 
     IsObject cIndex("#" & vElem) 
     If Err.Number <> 0 Then 
      vRetVal(lIdx) = vElem 
      lIdx = lIdx + 1 
     End If 
     On Error GoTo 0 
    Next 
    '--- shrink output array 
    If lIdx = LBound(vRetVal) Then 
     vRetVal = Split(vbNullString) 
    Else 
     ReDim Preserve vRetVal(0 To lIdx - 1) As Variant 
    End If 
    SubstractArray = vRetVal 
End Function