Question

I've tried to figure this issue out on my own, but perhaps there is something I am misunderstanding about the way ArrayUnique works.

Here is some sample LotusScript code:

'Let's test some dates
dateOne = CDat("12/16/2010")
dateTwo = CDat("12/16/2010")
testSuccess = (dateOne = dateTwo)

'On evaluation, testSuccess = true

'Now let's make an array ...
Dim someArray(1) As Variant
someArray(0) = dateOne
someArray(1) = dateTwo
uniqueArray = ArrayUnique(someArray)

'uniqueArray has the same two elements ... the duplicate hasn't been removed

In the above example, dateOne, dateTwo, testSuccess, and uniqueArray are all implicitly-declared variant variables.

What am I doing wrong? I've read in the help where it says:

Usage

Elements in a variant array will only compare equal if they are of the same type. The variant array can't contain classes or objects.

Array elements that contain the null value will match other null values.

Array elements that are empty will match with other elements that are empty.

Well, the variant array in this example contains variant variables that are of a date/time type. So, if I'm reading this correctly, I'm not doing anything wrong.

Edit: On the Notes Forums, user Thoams Kennedy tried the following:

If you spell out the time component like this

dateOne = CDat("12/16/2010 04:20:17 AM")

dateTwo = CDat("12/16/2010 04:20:17 AM")

it will still treat them as distinct. There does not seem to be a millisecond component, so I would say that ArrayUnique does not know how to deal with DateTime variants.

So his conclusion is that ArrayUnique umm, doesn't work.

Was it helpful?

Solution

I did some testing and it looks like ArrayUnique just can't handle LS data type 7 (Date/Time). Internally the date types are stored as doubles so you can convert to and from a double to get this to work.

For example:

'Now let's make an array ... 
Dim someArray(1) As Variant 
someArray(0) = Cdbl(dateOne)
someArray(1) = Cdbl(dateTwo)
uniqueArray = ArrayUnique(someArray)

uniqueArray will only have one element.

Alternatively you can Dim dateOne and dateTwo as Doubles before you assign them the date values, and that seems to work fine too.

OTHER TIPS

Well, this works:

%REM
    Function ArrayUniqueStringCompare
    Since ArrayUnique doesn't seem to work in some cases (such as with date/time values),
    Let's convert all of the elements to string and then perform arrayunique.
    After performing unique, we can convert back to original type
    This will crash if sourceArray is not an array.
%END REM
Function ArrayUniqueStringCompare(sourceArray As Variant) As Variant
    typeOfElement$ = TypeName(sourceArray(0))
    upperLimitSource% = UBound(sourceArray)
    Dim stringArray() As String
    ReDim stringArray(upperLimitSource%)
    For i% = 0 To upperLimitSource%
        stringArray(i%) = CStr(sourceArray(i%))
    Next
    'Now get the unique values...
    uniqueArray = ArrayUnique(stringArray)
    upperLimitUnique% = UBound(uniqueArray)
    'Finally, convert the values back to their original data type
    Dim returnArray() As Variant
    ReDim returnArray(upperLimitUnique%)
    For i% = 0 To upperLimitUnique%
        If typeOfElement$ = "DATE" Then
            returnArray(i%) = CDat(uniqueArray(i%))
        End If
    Next
    ArrayUniqueStringCompare = returnArray
End Function

But surely this can't be the best solution, right? There must be some better way of getting ArrayUnique to work...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top