API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
@Unique LotusScript Function
This function mimics the @Unique function. It takes an array as input and returns a variant array containing all the unique elements of the source array. To use, make a call like this:
NewArray = UniqueElements(SourceArray)

Here is the source code:

Function UniqueElements(PassedArray() As String) As Variant
   Dim TempList List As Integer
   Dim NewArray() As String
   Dim ArrayElement As String
   Dim i As Integer
   Dim Counter As Integer
   ' This takes the string array passed into the variable and assigns it
   ' to a list called "TempList"
   For i = Lbound(PassedArray) To Ubound(PassedArray)
      ArrayElement = PassedArray(i)
      TempLis(ArrayElement) = 1
   Next
   ' This takes the new list and puts it back into an array
   Counter = Lbound(PassedArray)
   Forall IndEntry In TempList
      Redim Preserve NewArray(Counter)
      NewArray(Counter) = Listtag(IndEntry)
      Counter = Counter+1
   End Forall
   ' This returns the new array
   UniqueElements = NewArray
End Function

This can also be done using the Evaluate statement along with the @Unique function:

First, in your script you'll want to have an array. Let's say it's contained in a variable array. Now, you want to find all the unique elements of that array. You know that in formula language, you can say @Unique(array) to get the results. But that won't work in LotusScript since Evaluate needs to have a true Notes formula. You know that a field name can be passed to the @Unique function, so let's build a field. First, build a temporary document. You don't care about the document -- it's only being used to contain a field. The document never has to be saved to disk. After the document is created, the put the array into a field on the document. Now, you have a field and you can call @Unique to give you the unique elements of the array. Here's a code snippet:

Set tempdoc = db.createdocument
Set tempitem = New notesitem(tempdoc, "tempArray", array)
newarray = Evaluate("@Unique(tempArray)", tempdoc)

After those 3 statements, newarray (defined as a Variant) will have the unique elements in the LotusScript array.

The trick comes from the optional second parameter to Evaluate, which is a notes document.

This technique can be used for lots of things which script can't easily do -- anything involving working on multiple entries of an array at the same time.