Reg Exp
Web Design
Notes Client
Use Split/Join To Remove Entry From A List
Recently I was working with arrays in LotusScript. Each element of the list was a text string. I had a need to remove an element from the list. There are lots of different ways to do this, and here is one more.

This technique takes the elements of the array, then blanks out the one to be removed. Then it takes the entire array and builds a long text string out of it. Then the text string is manipulated, and the resulting string is converted back into an array. The new array will have the blank entry removed. Obviously, this technique relies on the size of the text string, so you wouldn't want to do this with large arrays. But it can also be used whether you're removing one entry, or multiple entries, from your source array.

The concept is that you create a variant array with the values, then blank out the value to be removed. Let's just provide an example as a starting point:
Dim values As Variant
Redim values(2)
values(0) = "A"
values(1) = "B"
values(2) = "C"

(Obviously, the code above is quite inefficient. Ideally, instead of re-dimming and hard-coding the values, they would be coming from somewhere else, like a multi-value field on a document).

Next, blank out the value or values that are to be removed: values(2) = ""

(Again, this would not be hard-coded. The most likely scenario would be something where the one to be removed was chosen from a list).

Next, comes the real meat of the code. This is where the new array is built, with all the blank elements removed.
Dim temp As String
temp = Join(values, "~~~")
While Instr(temp, "~~~~~~") <> 0
   temp = Strleft(temp, "~~~~~~") & "~~~" & Strright(temp, "~~~~~~")
If Left(temp, 3) = "~~~" Then temp = Strright(temp, "~~~")
If Right(temp, 3) = "~~~" Then temp = Strleftback(temp, "~~~")
values = Split(temp, "~~~")

The first statement makes a long string (using the Join statement with a known separator that isn't going to appear anywhere in the array elements. I chose three "~" characters as my known separator - you could choose another if you wanted.

The next statement is relevant if an element somewhere in the middle of the array was removed. That element will have consecutive separators. In my case, "consecutive" means "two groups of three". So my statement looks for six consecutive tilde characters. If you have a different separator, this will be two of the separators consecutive. The code replaces every consecutive occurrence with a single occurrence. So if multiple elements were removed from the middle of the array, all the blanks will be removed in the final array.

The next two statements are relevant if the first or last element of the array was removed. If the first element was removed, our long string will start with the separator, so that needs to be ignored. If the last element was removed, our long string will end with the separator, so that needs to be ignored.

Finally, we have a long string. The separator does not appear at the start or the end, and there are not any consecutive separators. So the long string can be split up and the values array re-populated. The array will now have no blank values - all that is left is the non-blank values from the original array.

Like I said earlier, this may not be the most efficient way of eliminating values from an array, and it certainly shouldn't be used for large arrays, but it's pretty quick and uses built-in LotusScript methods to do all the work.