The HotDocs Computation Archive

0096 - Single-line Address from a Multi-line Address


Description:

How to reformat a multi-line address as a single-line address.

 

• Code •

// Start with the whole address
Address-t

// Turn the first line break into a comma
SET Temp-n TO POSITION( RESULT, "\r" )
IF Temp-n > 0
   FIRST( RESULT, Temp-n - 1 ) +
   ", " +
   LAST( RESULT, LENGTH( RESULT ) - Temp-n - 1 )
END IF

// Turn the second line break into a comma
SET Temp-n TO POSITION( RESULT, "\r" )
IF Temp-n > 0
   FIRST( RESULT, Temp-n - 1 ) +
   ", " +
   LAST( RESULT, LENGTH( RESULT ) - Temp-n - 1 )
END IF

// Turn the third line break (if any) into a comma
SET Temp-n TO POSITION( RESULT, "\r" )
IF Temp-n > 0
   FIRST( RESULT, Temp-n - 1 ) +
   ", " +
   LAST( RESULT, LENGTH( RESULT ) - Temp-n - 1 )
END IF

// Turn the fourth line break (if any) into a comma
SET Temp-n TO POSITION( RESULT, "\r" )
IF Temp-n > 0
   FIRST( RESULT, Temp-n - 1 ) +
   ", " +
   LAST( RESULT, LENGTH( RESULT ) - Temp-n - 1 )
END IF

• Explanation •

Addresses in HotDocs are often handled by a single, multi-line field that holds the complete address. While this is a simple approach, it can make things difficult when a single-line address is needed.

This computation takes a multi-line address field and returns the address in single-line format. So,

Acme Consulting
Box 945
123 Some Street
Anytown, USA 12345

becomes

Acme Consulting, Box 945, 123 Some Street, Anytown, USA 12345

The computation first retrieves the value in the text field Address-t. It then begins looking for line breaks ("\r"). Each time it finds a line break, it separates the text before and after the line break, and then reassembles them with a separating comma. This computation will remove up to four line breaks, which should be sufficient.

The computation requires a temporary number variable, Temp-n, which is used to store the character position of the line break.

As an alternative, you could use the looping technique described in Computation #0015 - Loops via REPEAT to locate and remove the line breaks. An example is provided in Computation #0130, Multi-line to Single-line. That method is used in the model template below.

NOTE: In determining where a line break occurs, it is important to remember that a line break is actually two characters long (line feed character + carriage return character).

 

• 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