Pages

Tuesday, 18 October 2011

Compare the contents of two arrays


' Example usage

sA = Array("A""B""D")
sB = Array("A""C""B")


MsgBox CompareArrays(sAsB)

 

 

' =============================================================
' function: CompareArrays
' desc : Compares the content of two arrays and checks that
'        they each contain the same data, even if in a 
'        different order
' params : arrArray1 is the base array
'          arrArray2 is the array to compare
' returns : True if they contain same data, False otherwise
' =============================================================
Function CompareArrays(arrArray1arrArray2)

Dim intA1
Dim intA2
Dim blnMatched

' check that the arrays are the same size
If UBound(arrArray1<> UBound(arrArray2then

    ' arrays are different size, so return false and exit function
    CompareArrays = False
    Exit Function

End if

' for each element in the first array
For intA1 = LBound(arrArray1to UBound(arrArray1)

    ' initialise this to false
    blnMatched = False

    ' for each element in the second array
    For intA2 = LBound(arrArray2to UBound(arrArray2)

        ' compare the content of the two arrays
        If arrArray1 (intA1) = arrArray2 (intA2Then
            blnMatched = True
            Exit For
        End If

    Next ' next element in second array

    ' if the element was not found in array two, return false and exit function
    If Not blnMatched then 
        CompareArrays = False
        Exit Function
    End If

Next ' next element in first array

' if the function got this far, then the arrays contain the same data
CompareArrays = True

End Function ' CompareArrays


0 comments:

Post a Comment