dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB7
Expertise: Intermediate
Oct 13, 2003

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


ArrayFilter - Filtering arrays of any type, with many options

' Filter the input array. It can work with string arrays or arrays of other 
' type!
' The input array is modified by removing the elements that do not match the 
' filter options, but the array is not actually re-sized. The filter results 
' will be moved at the beginning of the array, and empty elements will be left 
' at the end. The function returns the number of elements that match the filter 
' options however, so that you can manually redim the array if you want to.
' 
' Example:
'   Dim arr() As String = {"this is a String", "this is another one",
'  "test string", "will it work?", "ok"}
'   ' include all the elements that have "string" within them
'   Dim numElements As Integer = ArrayFilter(arr, "string", False, False, True)
'
'   Dim i As Integer
'   Debug.WriteLine("--- Start ---")
'   For i = 0 To numElements - 1
'       Debug.WriteLine(arr(i))
'   Next
'   Debug.WriteLine("--- End ---")
'
'   ' exclude all the 4 elements
'   Dim arr2() As Integer = {1, 4, 7, 9, 11, 43, 56, 4, 11, 1}
'   numElements = ArrayFilter(arr2, 4, False, False, False)
'
'   Debug.WriteLine("--- Start ---")
'   For i = 0 To numElements - 1
'       Debug.WriteLine(arr2(i))
'   Next
'   Debug.WriteLine("--- End ---")

Function ArrayFilter(ByVal arr As Array, ByVal search As Object) As Integer
    Return ArrayFilter(arr, search, True)
End Function

' This overloaded version adds the possibility to choose whether the search is 
' case sensitive or not
Function ArrayFilter(ByVal arr As Array, ByVal search As Object, _
    ByVal caseSensitive As Boolean) As Integer
    Return ArrayFilter(arr, search, caseSensitive, True)
End Function

' This overloaded version adds the possibility to choose whether partial search 
' results are accepted or not
Function ArrayFilter(ByVal arr As Array, ByVal search As Object, _
    ByVal caseSensitive As Boolean, ByVal exactMatch As Boolean) As Integer
    Return ArrayFilter(arr, search, caseSensitive, exactMatch, True)
End Function

' This overloaded version adds the possibility to choose whether the search 
' results are included or excluded in/from the filtered array
Function ArrayFilter(ByVal arr As Array, ByVal search As Object, _
    ByVal caseSensitive As Boolean, ByVal exactMatch As Boolean, _
    ByVal include As Boolean) 
As Integer
    Return ArrayFilter(arr, search, caseSensitive, exactMatch, include, _
        arr.GetLowerBound(0), arr.GetUpperBound(0))
End Function

' This overloaded version adds the possibility to choose the portion of the 
' array to be filtered
Function ArrayFilter(ByVal arr As Array, ByVal search As Object, _
    ByVal caseSensitive As Boolean, ByVal exactMatch As Boolean, _
    ByVal include As Boolean, 
ByVal first As Integer, ByVal last As Integer) As Integer
    Dim strSearch As String
    Dim removeElement As Boolean
    Dim isStringArr As Boolean
    Dim numRemoved As Integer

    ' if this is a String array,
    If TypeOf arr Is String() Then
        isStringArr = True
        ' convert the search object to a string, and if the caseSensitive 
        ' option is not True, convert it to lower case
        strSearch = search.ToString()
        If Not caseSensitive Then strSearch = strSearch.ToLower()
    End If

    Dim i As Integer
    For i = last To first Step -1
        removeElement = False
        ' if this is a string array...
        If isStringArr Then
            ' ...convert the array's item to string, and to lower case is the 
            ' filter is not case sensitive
            Dim strValue As String = CType(arr.GetValue(i), String)
            If Not caseSensitive Then strValue = strValue.ToLower()

            If exactMatch Then
                ' if this is an exact match, remove or leave the element 
                ' according to the include option
                If strSearch = strValue Then
                    If Not include Then removeElement = True
                Else
                    If include Then removeElement = True
                End If
            Else
                ' if this is a partial match, remove or leave the element 
                ' according to the include option
                If strValue.IndexOf(strSearch) > -1 Then
                    If Not include Then removeElement = True
                Else
                    If include Then removeElement = True
                End If
            End If
        Else
            ' if not a string array, use the Object's Equals method for value 
            ' comparison
            Dim oValue = arr.GetValue(i)
            If oValue.Equals(search) Then
                If Not include Then removeElement = True
            Else
                If include Then removeElement = True
            End If
        End If

        If removeElement Then
            ' shift elements from arr(index+1) to arr(index)
            Array.Copy(arr, i + 1, arr, i, arr.GetUpperBound(0) - i)
            ' clear the last element
            arr.Clear(arr, arr.GetUpperBound(0), 1)
            ' increment the counter of removed elements
            numRemoved += 1
        End If
    Next

    Return arr.Length - numRemoved
End Function
Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date