LotusScript ArrayUnique function doesn't seem to work with date and time arrays

I tried to solve this problem myself, but maybe I donโ€™t understand something about how ArrayUnique works.

Here is a sample LotusScript code:

 'Let test some dates dateOne = CDat("12/16/2010") dateTwo = CDat("12/16/2010") testSuccess = (dateOne = dateTwo) 'On evaluation, testSuccess = true 'Now let 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 implicitly declared variant variables.

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

Using

Elements in an alternative array will only compare if they are of the same type. The variant array cannot contain classes or objects.

Array elements containing a null value will correspond to other null values.

Array elements that are empty will match other empty elements.

Well, the array of variants in this example contains variant variables that have a date / time type. So, if I read this correctly, I am not mistaken.

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

If you specified a time component like this

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

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

he will still treat them as a separate one. There seems to be no millisecond component, so I would say that ArrayUnique does not know how to deal with DateTime variants.

Therefore, its conclusion is that ArrayUnique umm is not working.

+4
source share
2 answers

I did some testing, and it looks like ArrayUnique just can't handle LS type 7 data (date / time). Internally, date types are stored as doubles, so you can convert them to and from doubles to make it work.

For instance:

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

uniqueArray will have only one element.

Alternatively, you can Dim dateOne and dateTwo as Doubles before assigning them date values, and this seems to work too.

+2
source

Well, this works:

 %REM Function ArrayUniqueStringCompare Since ArrayUnique doesn't seem to work in some cases (such as with date/time values), Let 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 of course, this may not be the best solution, right? There must be some better way to get ArrayUnique to work ...

+2
source

Source: https://habr.com/ru/post/1332395/


All Articles