The @Unique function in Formula is a handy way of removing duplicates. This LotusScript function allows you perform the same function in LotusScript.
The following LotusScript function uses a list data type to unique the values passed to it. In LotusScript there is a list variable type. A list is similar to an array in that it holds multiple values but each value has a tag or index associated with it. Unlike other languages which use collections, the LotusScript list does not throw an error if you attempt to set the value of an existing list element. Rather it replaces the existing value with the new value. If the tag or index doesn't exist a new one is created and the value placed there.
Using this characteristic stops you from having to search an array or collection every time you add a new element to see if it is unique. The following code also implements an 'Elements' function which mimics @Elements.
The error trapping is designed to provide trace back functionality if an error occurs.
To use this function call it by:
Dim myUniqueArray as variant myUniqueArray = Unique( MyNonUniqueArray)
Function Unique(sourceList As Variant) As Variant On Error Goto errorHandle Dim uniqueList List As Variant Dim returnArray() As Variant ' return value Dim count% ' Check to see we have more than one value If Isscalar( sourceList ) Then Unique = sourceList Else ' Unique the values Forall v In sourceList uniqueList( Cstr( v ) ) = v End Forall 'v in sourceList Redim returnArray( 0 To Elements( uniqueList ) -1 ) ' Set the return array to be zero based 'Turn the values into an array count% = 0 Forall u In uniqueList returnArray( count% ) = u count% = count% + 1 End Forall 'u In uniqueList Unique = returnArray End If 'isscalar Exit Function errorHandle: Error Err, "[TextLib] [Unique] (" & Cstr( Erl ) & ")" & Chr$(13) & Error$ Exit Function End Function Function Elements( values As Variant) As Integer On Error Goto errorHandle Elements = 0 ' Check to see we have more than one value If Isscalar( values ) Then Elements = 1 Else 'Non scalar value Forall v In values Elements = Elements + 1 End Forall End If 'isScalar Exit Function errorHandle: Error Err, "[TextLib] [Elements] (" & Cstr ( Erl ) & ")" & Chr$(13) & Error$ Exit Function End Function
Andre Guirard wrote a bunch of @Function conversions to LotusScript and posted them to Notes.net in the library "Design Library Repository" back before the turn of the millennium. In this library is a non-case sensitive @Unique function that I still use today. I would recommend this function as another option to consider for the Unique function.
Do you have comments on this tip? Let us know.
This tip was submitted to the SearchDomino.com tip exchange by member Shane Hollis. Please let others know how useful it is via the rating scale at the end of the tip. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.
This was first published in December 2004