The HotDocs Computation Archive

0015 - Loops via REPEAT


Description:

How to create and use a basic loop with a REPEAT.

 

• Code •

Sample Code:

(counts the occurances of the letter "a" in a string)

""
SET Temp-n TO LENGTH( TextVar )
SET LoopLimit-n[ Temp-n ] TO 1
SET Count-n TO 0

REPEAT Loop
   IF MID( TextVar, COUNTER, 1 ) = "a"
      SET Count-n TO Count-n + 1
   END IF
   SET LoopLimit-n TO UNANSWERED
END REPEAT

"There were «Count-n» occurances of the letter 'a'."

• Explanation •

To create a loop, set up the following:

A repeated dialog called Loop, containing a single number variable called LoopLimit-n. (Be sure to set this variable's Advanced options to "Ask only in dialog", "Don't warn if unanswered", and "Don't save in answer file".)

Also create a second number variable called Temp-n. Set its Advanced options just like those for LoopLimit-n, but do not put this variable in the Loop dialog.

How it works: The loop cycles a predetermined number of times. The number of times it should loop is set in the number variable Temp-n. In that example, we set it to the length of a text variable, TextVar. This will let us step through that variable letter by letter, then quit after the last letter.

To make sure that we loop exactly Temp-n times, we give the Loop dialog's only variable, LoopLimit-n, a value at the Temp-n answer position. This makes HotDocs think that LoopLimit-n has Temp-n answers to cycle through. Once it has cycled through these answers, it will exit with the END REPEAT. We then reset the LoopLimit-n variable to UNANSWERED so that the loop can be used again.

While we are inside of the REPEAT, we can use COUNTER to tell us what pass we are on. In the example above, the first pass produces a COUNTER value of 1. So we look at letter 1 of the text string to see if it is the letter "a". If it is, we can increase a counter variable, such as the number variable Count-n, by 1. By the time we have looped through the whole string, Count-n will contain an accurate count of how many times "a" was found.

Note that this is, for all intents and purposes, a limited FOR ... NEXT loop, where the number of iterations is fixed at the outset. (It is limited in the sense that it will only increment, and the increment is always +1). I usually set the number of iterations to the length of a text string, but there's no reason that the loop should be limited to analyzing text strings.

See also, Computation #0082: Loops via Recursion.


•  •  •  •  •  •  •

A Caveat

This is an "unsupported method," meaning that Capsoft does not guarantee that it will continue to be supported in future versions. It has been reported that this looping technique does not work with HotDocs Online. However, it has continued to be supported in HotDocs Pro and Standard through the current version, 5.4 - the last release before HotDocs 6.0 (which has been rumored to include an official and supported looping instruction).

 

• Contributors •

LegalCS