Reg Exp
Web Design
Notes Client
Missing From Array
After using the Are Arrays Equal function in LotusScript for a while, a comment came from one of our developers: "This is good and all, but how do I know what is different about the two arrays?" The other function simply exits when finding a difference. So it makes sense to just keep track of the differences and then return those differences somehow. But there's two types of differences:
We decided to push back the reporting of differences one level to the developer using the reusable code. So this reusable code tells you the elements in array 2 (the second parameter) that are missing in array 1 (the first parameter). If you have two arrays "a" and "b" you can call this function twice, swapping the parameters, to find all the differences. But how you report on the differences is up to you. The function returns an array of elements that are missing in array 1 or a string if there are no differences or there was an error (if one of the parameters wasn't actually an array, for example).

The function takes it code from the Are Arrays Equal function, and slightly modifies it. It also only needs to go though once, since we don't care if all the elements in array 1 are in array 2 (we're only checking the one way). It also doesn't exit once a difference is found. Instead, that difference is placed into the array to be returned.

Function MissingFromArray(baselineArray As Variant, compareArray As Variant) As Variant
   Dim i As Integer
   Dim key As String
   Dim tempList List As Integer
   Dim results As Variant
   Dim x As Integer
   If Not Isarray(baselineArray) Or Not Isarray(compareArray) Then
      Exit Function
   End If

This code first makes sure that both parameters are arrays. This is something that is necessary for a reusable function.

   For i = Lbound(baselineArray) To Ubound(baselineArray)
      key = Typename(baselineArray(i)) & "~" & Cstr(baselineArray(i))
      If Iselement(tempList(key)) Then
         tempList(key) = tempList(key) + 1
         tempList(key) = 1
      End If

Build a list containing the data type of the element (so we can differentiate between the number 1 and the string "1") followed by the value. For elements that are duplicated, keep track of the number of appearances of the value. This list is then used to compare to the second array.

   For i = Lbound(compareArray) To Ubound(compareArray)
      key = Typename(compareArray(i)) & "~" & Cstr(compareArray(i))
      If Not Iselement(tempList(key)) Then
         If Not Isarray(results) Then
            Redim results(1) As String
            x = Lbound(results)
            Redim results(x)
            x = Ubound(results)+1
            Redim Preserve results(x)
         End If
         results(x) = Cstr(compareArray(i))
         tempList(key) = tempList(key) - 1
         If tempList(key) = 0 Then Erase tempList(key)
      End If
   MissingFromArray = results
End Function

Go through the second array. If the value is a match for something in the first array, remove it from the list built from the first array. (If the element is in the first array multiple times, just subtract 1 from the value). If the value is not in the first array, then add it to the results array. Note that the code above is converting everything to strings for the results array. If you truly want the values, then the line:
Redim results(1) As String
should be:
Redim results(1) As Variant
and when keeping the results, don't convert it to string. The line should instead be:
results(x) = compareArray(i)

We chose to convert everything to strings because this function is going to be used most often to report on the differences betwen arrays.

At the bottom of the function, the variable results either has never been defined (never been redimmed) if there were no differences or it's an array. If it's never been redimmed, then Variant variables by default get an empty string value, so an empty string will be returned. If an array is returned, then the array holds the elements in array 2 that are absent from array 1.