Ls Explode/Implode -- The Easy Way

Answer truthfully; Do you like working with text lists in LotusScript? NO
WAY.

I always seem to get into the jam of adding/removing items from a text list.
In the formula language, it's really easy. Commands like @Explode and @Implode
make life relatively simple.

In the jungle of LS, you have to define a variant, get the field, and then the
real fun begins. REDIM PRESERVEs, UBOUNDs looping thru all the values, and
then the nightmare of removing empty list elements. How about uniqueness? And
what about using the extended class syntax (you know doc.fieldx="some text")?
YIKES!!!

While LS may not be good at using text lists, it has great string manipulation
tools. Why not use all those good commands like LEFT,RIGHT,MID,INSTR.. etc...
And forget the drudgery of using NotesItem to 'AppendToTextList'. With the
following 2 functions, you can turn those text lists into strings, change em to
your hearts content, then put them back as lists.

LSExplode and LSImplode work like their @function brothers @Explode and
@Implode (LSImplode actually goes one step further by optionally @UNIQUEing the
list). You can use the extended class syntax with these functions, and...
they are simpler than the Variant Array option.

The following will work with any release of Domino over 4.5x, however I don't
recommend running this on Domino versions before 4.6.1. Releases previous to
4.6.1 had a memory leak calling the Evaluate command which will eventually
crash your Domino server
(see:http://support.lotus.com/sims2.nsf/802ee480bdd32d0b852566fa005acf8d/0714cf2
68a40d203852565140070e0d8?OpenDocument)

-Michael DeMaria / PCLP
Sub Initialize

' This Initialize routine demonstrates LSExplode and LSImplode:
' to use this example, create a new form called "Demo",
' and put 3 fields on it:
' Text1 = Editable Text
' List1 & List2 = Editable Text, ALLOW MULTI VALUES (important).
' Then create an agent that is run from the action menu/Run Once
' (@ commands may be used), select Lotusscript Insert this
' code into the Initialize event, and create the 2 functions
'LSImplode and LSExplode.
'
' For more information, see the comments within LSExplode and
' LSImplode. You may also want to explore Notes Help on
' @Explode and @Implode.
'

Dim doc As NotesDocument
Dim s As NotesSession
Dim db As NotesDatabase
Dim ThisTest As String
Dim TestString As String

Set s = New NotesSession
Set db = s.CurrentDatabase
Set doc = New NotesDocument(db)
doc.form = "Demo"

'Here is a string of 3 people we want to make into a list
ThisTest = "joe;steve;sally"

' set the NON list field. Nothing earth shattering here....
doc.Text1 = ThisTest

'OK... Now take the field, and turn it into a text list into LIST1
doc.List1 = LSExplode(ThisTest,";",True,False,doc)

'Great. Now get the field from List1, Implode it, add mike, remove joe
'and put it into text2 -->

' 1. Get the existing list as a string
TestString = LSImplode(doc.List1,";")
' 2. Add Mike (note that you have to use the separator ";"):
TestString = TestString & ";Mike"
' 3. Get Rid of joe:
TestString = Mid$(TestString,5)

'almost done! Put the new list into List2. Note that we are
'removing empty elements this time
doc.List2 = LSExplode(TestString,";",False,False,doc)

Call doc.save(True,False)


End Sub


Function LSExplode(tmp As String,sep As String,IncludeEmpty As Integer, _
MakeUnique As Integer, doc As NotesDocument) As Variant

'This function takes a string and returns a variant that behaves
'like a Text List. This Variant can be assigned to a back end
'extended class syntax field [ie doc.field=LSExplode(...], or can
'be set using the .values property of NotesItem if desired
'
'Syntax:
' SomeVariant = LSExplode(String,Separator,IncludeEmpty,MakeUnique,doc)
'
'Parameters: (all are REQUIRED)
'
'String
' String. The string whose contents you want to make into
' a text list.
'
'Separator
' String. A char (or chars) that is used to define the end
' of an element
'
'IncludeEmpty
' Boolean. True to include empty elements. False to remove.
'
'MakeUnique
' Boolean. True to remove duplicates, False to keep all
'
'doc
' NotesDocument. Required for handle to Evaluate statement.
' NOT MODIFIED in any way.
'
'Example:
' SomeVariant = Explode("John;Sue;Joe",";",true,true,doc)
' doc.fieldx = Explode("John;Sue;Joe",";",true,true,doc)
' item.values = Explode("John;Sue;Joe",";",true,true,doc)
' > 4.5x, 4.6.1 bug fixed
'
'For more information use the Notes help database and search
'

Dim exc As String
exc = ""

If MakeUnique Then ' add @unique
exc = exc & "@unique("
End If

'Begin building the string to execute
exc = exc & |@Explode("| & tmp & |";"| & sep & |";|

If IncludeEmpty Then ' have to include/toss empties
exc = exc & "@Yes"
Else
exc = exc & "@No"
End If

exc = exc & ")" 'close @explode

If MakeUnique Then ' close @unique
exc = exc & ")"
End If

exc = exc & ";" ' just to be nice

LSExplode = Evaluate(exc,doc)

End Function

Function LSImplode(tmp As Variant, sep As String) As String
Dim x As Integer

'This function takes an array and returns a string where
'the original array elements are separated by sep.
'The string can then be modified using regular LotusScript
'commands (IE, L

Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close