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
Comments
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
Posted by Karl-Henry Martinsson At 12:52:08 On 27/08/2009 | - Website - |
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.
Posted by Tim Tripcony At 12:52:23 On 27/08/2009 | - Website - |
Posted by Kevin Pettitt At 13:05:03 On 27/08/2009 | - Website - |
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.
Posted by John Vaughan At 13:39:22 On 27/08/2009 | - Website - |
licno = "123XYZ"
Set car(licno) = New CarData()
Call car(licno).AddDriver("Karl-Henry Martinsson")
Call car(licno).SetColor = "Silver"
xmldata = car(licno).GetXML()
Posted by Karl-Henry Martinsson At 14:16:06 On 27/08/2009 | - Website - |
Enjoy!
Posted by Karl-Henry Martinsson At 15:36:07 On 27/08/2009 | - Website - |
Posted by Kathy Brown At 16:23:00 On 27/08/2009 | - Website - |
I get there too, with no Maps or Class ;).
It had to be written.
Posted by Yancy Lent At 09:29:54 On 28/08/2009 | - Website - |
#FatAlbert
Posted by John Vaughan At 09:58:14 On 28/08/2009 | - Website - |