SNTT: Lists – the forgotten gem in Lotusscript

OK, I know it is Friday already, but here is my Show-n-Tell-Thursday entry…

Lists are a data type that I don’t think is used enough. Many Notes developers, even some that been programming in the environment for years, don’t use this very powerful feature. In previous years, Bill Buchan have been talking about lists in some of his Lotussphere sessions, and when asking how many in the audience that used lists, less than 10% raised their hands.

So what are lists? They are a data type, similar to an array, but instead of using a numeric index, it uses a string as index, or tag as it is called.

This makes it easy to address items in the list, check if they exists, etc.

Here is a simple example:

Dim age List as Integer

Dim name as String

age( "karl" ) = 38

age( "erik" ) = 7

name = InputBox$( "Enter name:" )

If IsElement(age(Lcase(name))) = True Then

MsgBox name & " is " & age(lcase(name)) & " years old."

Else

MsgBox name & " does not occur in the list."

End If

This code define a list of integers, containing the age of two people. When a name is entered in the input box, IsElement is used to check if an item with that name is found in the list. If it is, the age is displayed, if not another message is displayed, saying that the name was not found.

Note that the list tag (name in this case) is case sensitive! 

To loop through a list and display all the values, use ForAll. Here is an example where we loop thorugh all the entries and display their list tags as well as the values:

Dim age List As Integer
age( "Karl" ) = 38
age( "Erik" ) = 7
Forall a In age
Msgbox Listtag(a) & " is " & a & " years old."
End Forall

Some of the things I use lists for are web agents, where I parse the URL passed for parameters, and build a list of them. Then it is very easy to write code that check for parameters and get the value if they exist.

I wrote an article about lists for The View, published in November/December 2006. You can find more details and examples there.

 

Leave a Reply