The HotDocs Computation Archive
Get Extra Help

0056 - Reverse Text

Description:

Inverts a string of text or a text variable (reverses it).


• Code •

// Set up the loop
SET Temp-n TO LENGTH( TextVar )
SET LoopLimit-n[ Temp-n ] TO 1

""
REPEAT Loop
   RESULT + MID(
      "«TextVar»,"
      Temp-n - COUNTER + 1,
      1 )
END REPEAT

// Reset the variables
SET LoopLimit-n[ Temp-n ] TO UNANSWERED
SET Temp-n TO UNANSWERED


• Explanation •

This computation takes a text variable or a string of text and reverses it. For example, "mississippi" is returned as "ippississim."

This can be a useful tool for extracting values from strings. For example, if you wished to take the filename portion off of a fully-qualified pathname, such as c:\folder\folder\folder\filename.xxx, you would not be able to determine where the filename begins. The POSITION model could tell you where the first "\" is located, but this is of little help. But by reversing the string to be xxx.emanelif\redlof\redlof\redlof\:c, you can find the location of the first "\" and know that everything up to that point is the filename. You would then extract this value and reverse it to get the filename.

The computation requires a repeated dialog called Loop. This dialog has a single temporary number variable in it called LoopLimit-n. The mechanics of the Loop are described in Computation #0015: Loops Via REPEAT. The computation also requires a temporary number variable named Temp-n. Both of these temporary variables should have their Advanced options set to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file."

The computation uses the repeated dialog Loop to cycle through each letter of the text variable TextVar in reverse order. Each pass through the loop appends another letter from the string. The reverse order is achieved by subtracting the current COUNTER value from the total number of letters and adding one. For example, the word "mississippi" has a length of 11 characters. The first pass will extract character 11 (length) minus 1 (counter) plus 1, or 11. The second pass will extract character 11 (length) minus 2 (counter) plus 1, or 10. And so on.

 

• Model Template •

This template has everything you need set up and configured for you. It will work as-is, or can be adapted to your variable and dialog names. It contains: 1) sample Word and WordPerfect templates (or an Automator form) to demonstrate an implementation of the computation, 2) a component file containing the computation and all supporting dialogs and variables, and 3) instructions for adapting the computation for your use.

(Go to the download page)

 
 

• Contributors •

LegalCS