Home > LotusScript, Notes Designer > Use LotusScript Split/Join To Remove Entry From A List

Use LotusScript 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, “~~~~~~”)
Wend
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.

Viewed 10816 times by 3785 viewers

  1. Gnaus
    October 21st, 2011 at 14:38 | #1

    Why don’t you use fulltrim ?
    FullTrim(array) will remove any empty entries in an array.
    This is native lotusscript and is way faster.

    The only situation where I do NOT recommand to use FullTrim is when cleaning filenames string lists. because FullTrim also remove duplicate whitespaces, I got some troubles trying to extract attached documents because some clever users got some attachement files names like “My file name that is sooo ooo oooo ooooo cool.doc” and the FullTrim will makes it “My file name that is sooo ooo oooo ooooo cool.doc” and if you try to detach this filename, the system will not find it in your doc.

    But that is quite rare and as long as you know what FullTrim exactly does, it will save you a lot of time and arrays manipulation.

    Regards

  2. Gnaus
    October 21st, 2011 at 14:46 | #2

    @Gnaus
    The “clever” system removed the extra whitespaces i’ve added in the 1st version of the filename i was giving as example on this comment.
    Just consider that “My      file        name      that is sooo ooo oooo      ooooo        cool.doc” as 1st version of the vilename (i hope ‘& n b s p’ will makes it)

  1. No trackbacks yet.