|
|
|
|
|
|
|
|
|
|
Using recursion in LotusScript (continued)
It's a little more complicated than our factorial example, but there are only six lines of code that require any real thought. Let's look at that If statement:
If Not Instr(currstring, replace) = 0 Then
|
The If statement is where we determine whether or not we've arrived at the end case. Are there any more back slashes present? If so, we launch into the recursive case:
lefttext = Left(currstring, Instr(currstring, replace)-1)
righttext = Right(currstring, Len(currstring) - (Instr(currstring, replace) - 1 + Len(replace)))
fulltext = lefttext + replacewith + righttext
replacesubstring = replacesubstring(fulltext, replace, replacewith)
|
The first three lines merely reassemble the string. Everything to the left of the text we want to replace is added to the text we want to replace it with. This string is then added to everything to the right. (If we were trying to replace back slashes with forward slashes in "Demo\TestDB.nsf", we'd add "Demo" to "/" and then add that to "TestDB.nsf".)
The fourth line in the recursive case is what truly makes this function recursive. We take the string we've just reassembled and check for more backslashes by recursively calling the ReplaceSubstring function.
The remainder of the important code consists entirely of:
replacesubstring = currstring
|
This is our end case, where we've finally reduced the problem to a point where it can be easily solved.
Drawbacks to using recursion While recursion can be a powerful tool, that power does come with drawbacks. The first drawback is that it takes some practice before you can start slamming out recursive code. If you're in a hurry to meet a deadline, you'll almost certainly use the Evaluate statement in LotusScript before you'd attempt to rewrite @ReplaceSubstring as a recursive function.
Another drawback is that recursive code may confuse those who aren't already familiar with recursion. Most Domino developers can muddle through someone else's LotusScript without too many problems (especially if it's well commented!). But looking at someone else's recursive code for the first time can be quite a trial.
Perhaps the biggest drawback to using recursion is the limit that Lotus Notes imposes on recursive functions. If your elegantly written recursive function needs to call itself too many times, you'll receive an "Out of Stack Space" error. This is because Lotus Notes has a 32K limit on the amount of information it stores in the stack for recursive functions. In other words, Lotus Notes won't be able to keep track of all of the temporary variables your function is using!
The first two drawbacks can be easily overcome, but that 32K-stack limit can be a real problem. Luckily, you can usually give yourself a little more stack space to play with by making fewer recursive calls. For instance, if we were to check for two back slash characters each time through our ReplaceSubstring function, we'd cut the number of recursive calls in half.
[ Prev | Next ]
|
|
|
|
|
|
-- Advertisement --
2-Minute Tutorials
How do I...
- integrate MS Office or OpenOffice with Notes?
- create cross-tab reports and charts?
- print serial letters and mailing labels?
- create PDFs in Lotus Notes?
Check out the 2-minute tutorials here. |
-- Advertisement --
SECURTRAC - MONITOR AND CONTROL YOUR DOMINO ENVIRONMENT
When it comes to your business, how do you ensure compliance with SOX, HIPAA or other industry driven regulations? Use SecurTrac to monitor and audit the life cycle of all objects in your Domino environment.
- Database Monitor
- Mail Monitor
- Domino Directory Monitor
- Notes.ini File Monitor
- Intrusion Detection Monitor
Click here for details and a free evaluation copy. |
|
|
|
|
|
|
|
|