Replace an Element in an Array
This function will substitute a single element in an array with a replacement value.
How It Works
The loop counter variable called i is initialized to zero. The function then loops through each element in the search array and compares the current element to the target value. If the element is found, the element value is replaced with the new value. All values are stored in a temporary array called ReturnArray, which is subsequently returned to the calling code.
Implementation
To implement this technique, add the following to a LotusScript library. Three values must be passed to this functionan array of values, the search value, and the replacement value.
Function ReplaceElement ( varFromList As Variant, varTarget As Variant, varReplacement As Variant) As Variant Redim ReturnArray(0) As Variant Dim i As Integer i = 0 Forall varElement In varFromList Redim Preserve ReturnArray (i) As Variant If varElement = varTarget Then ReturnArray(i) = varReplacement Else ReturnArray(i) = varElement End If i = i + 1 End Forall ReplaceElement = ReturnArray End Function
The following illustrates how this function could be called from an action button. In this example, team member Tom is replaced with team member Tammy (see Figure 13.1). The updated array is subsequently returned assigned to the newTeam array.
Sub Click(Source As Button) Dim newTeam As Variant Dim theTeam (0 To 2) As String theTeam (0) = "Tom" theTeam (1) = "Henry" theTeam (2) = "Mark" Msgbox Implode( theTeam, ", " ), , "Old Team" newTeam = ReplaceElement ( theTeam, "Tom", "Tammy" ) Msgbox Implode ( newTeam, ", "), , "New Team" End Sub
Figure 13.1. Replace an element in an array
Remember to include the Use LIBRARY statement in the calling design element if the function is stored in a LotusScript library.
Note
The ArrayReplace function could also be used to achieve a similar result. Refer to the Domino Designer help for additional information pertaining to this function.
Remove a Character from a String
|