«

»

Fun with Lists!

Lotusscript lists, that is. Frequently underutilized by developers (including me), but a very useful tool nonetheless. I had a need recently to loop through some documents, and create a unique list of names from those documents, and then count how many unique names I had. I needed to do it in Lotusscript. I would also need to do something with those names later in my code. Oh, and the number of names would depend on the document collection and how many were actually unique. Addtionally, I had more than one bit of data that I wanted to capture. For this example, we’ll say name and phone number. So, what to do?

Well, I *could* use an array, but not knowing how many names would be added to the array would mean having to ReDim the array, but that’s no fun. So enter Lists! Creating a list is super easy, and you don’t have to know how many items will be in the list. I added a little counter to count the number of items in my list as I added them. Yes, I could have added them, and then looped through afterward and counted them that way, but I actually had some other stuff to do, so did it when I added.

Dim clientList List As String ‘dim the list itself Dim n As Long ‘dim n as long for the counter n = 0 ‘start the count at 0 While Not entry Is Nothing ‘obviously prior code would have dimmed a view entry collection and 'provided a way to loop through it, this part of the code just insures we are acting on an actual entry ‘This next bit checks to see if the item is already in the list and if it is NOT, then increases the counter by 1, 'and adds the entry with column value 5 as the listtag and column value 6 as the value If Not (Iselement(clientList(entry.columnvalues(5)))) Then n = n + 1 clientList (entry.ColumnValues(5)) =entry.columnvalues(6) End If Print Cstr(n) ‘this prints the count of items added to the list ‘This just shows something we can do with the list, such as printing the listtag '(aka column value 5) for all of the entries in the list. Forall clients In clientList Print ListTag(clients) End Forall

ls2html was brought to you by nsftools.com

Pretty easy, huh? Easy to create and easy to use the data. So Lists! Use ‘em!

  • http://www.bleedyellow.com/blogs/texasswede Karl-Henry Martinsson

    Yes, lists are very powerful, and I use them all the time. I actually wrote an article about lists for the View (Nov/Dec 2006, that copy was actually distributed at Lotusphere 2007).

    Other cool things you can do with lists is to create lists of objects, either things like NotesDocuments or your own classes/data types.

    Example:

    Type CarData

    Manufacturer As String

    Year As String

    Color As String

    Owner As String

    End Type

    Dim car List As CarData

    car(“123XYZ”).Manufacturer = “Ford”

    car(“123XYZ”).Color = “Red”

    Then you can loop thought the list:

    Forall c in car

    Print c.Manufacturer & ” – ” & c.Color

    End ForAll

  • http://www.timtripcony.com Tim Tripcony

    Amen… very useful and, as you mentioned, underutilized.

    Oh, by the way, if you like Lists, you’ll adjust well to XPages: with JavaScript’s “associative arrays” and Java’s Maps, this type of storage model gets used constantly when doing any serious XPage development.

  • http://www.lotusguru.com Kevin Pettitt

    Love Lists myself, ever since Bill Buchan started beating us over the head a few years ago about them. Karl, I had not thought about using user defined types in that way but already see plenty of uses for them. Nice!

  • http://jonvon.net John Vaughan

    dear swede in tejas,

    man i gave up on Types. i always end up needing a method, seems like. i use Class instead.

    using Lists to keep track of objects FTW!

    tim,

    i say, nice mysterious hand waving over the XPages magic. ok so i do use associatvie arrays in JS. Maps? being a guy, i just don’t need maps. i’ll get there, it just takes me a few extra days.

  • http://www.bleedyellow.com/blogs/texasswede Karl-Henry Martinsson

    @4: Yeah, I use classes all the time as well, that makes it even more powerful.

    licno = “123XYZ”

    Set car(licno) = New CarData()

    Call car(licno).AddDriver(“Karl-Henry Martinsson”)

    Call car(licno).SetColor = “Silver”

    xmldata = car(licno).GetXML()

  • http://www.bleedyellow.com/blogs/texasswede Karl-Henry Martinsson

    I wrote a blog entry with some more production like code (using a class) here: { Link }

    Enjoy!

  • http://www.runningnotes.net Kathy Brown

    Thanks Karl! Great stuff!

  • http://www.yancylent.com Yancy Lent

    @John,

    I get there too, with no Maps or Class ;) .

    It had to be written.

  • http://jonvon.net John Vaughan

    Yancy, you’re like school in summer – you got no class! Emoticon

    #FatAlbert