The HotDocs Computation Archive
Get Extra Help

0130 - Multi-line To Single-line

Description:

Converts multi-line text to single-line text by replacing all returns with a comma and a space.


• Code •

TextVar
SET StrLen-n TO LENGTH( RESULT )
SET LoopLimit-n[ StrLen-n ] TO 1
ASK NONE
REPEAT Loop
   IF COUNTER < StrLen-n
      IF MID( RESULT, COUNTER, 2 ) = "\r"
         FIRST( RESULT, COUNTER - 1 ) + ", " + LAST( RESULT, StrLen-n - COUNTER - 1 )
      END IF
   END IF
   SET LoopLimit-n TO UNANSWERED
END REPEAT
ASK DEFAULT

• Explanation •

This computation takes the text from a text variable and strips out all of the returns, replacing them with a comma and a space. So the text:

ONE
TWO
THREE

becomes:

ONE, TWO, THREE

The computation requires the following:

The computation relies on the looping mechanism described in Computation #0015: Loops via REPEAT. Please refer to that page for some caveats about this method.

How it works. The mechanism is quite simple. The REPEAT steps through the string one character at a time, looking for a return. A return is represented by "\r" (see Computation #0012: Returns in Text Strings). We must examine both the current and the next character because a return is actually two characters long - a carriage return and a linefeed. If the next two characters are a return, we take the whole string up to the return, add a comma and a space, then take the whole string after the return.

The line "IF COUNTER < StrLen-n" makes sure that we don't test the last character of the string, since this cannot be a return (a return is two characters long). And the line "SET LoopLimit-n TO UNANSWERED" lets us clear out the Loop dialog as we go, making it ready for the next use.

See also, Computation #0020: Remove Spaces, Computation #0068: Strip Punctuation, and Computation #0141: Clean Up Spaces.

 

• 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