Tip

@Unique and @Elements for LotusScript

View member feedback to this tip.

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)

Code:

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

MEMBER FEEDBACK TO THIS TIP

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.

—Terry T.

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.