The HotDocs Computation Archive
Get Extra Help

0014 - Split Up a Name

Description:

Parses a full name into its component names.


• Explanation •

Preliminary Note: These computations are provided for those who have no choice but to split names. The preferred method, however, is to gather name elements into separate text variables to begin with, then stitch the name together to fit your immediate needs. For an example, see Computation #0034: Full Name.


•  •  •  •  •  •  •


• Code •

Sophisticated Parser:

""
SET FirstName-t TO ""
SET MiddleName-t TO ""
SET LastName-t TO ""
SET Suffix-t TO ""

// What kind of name is it?
IF FullName-t CONTAINS ","
   SET pos TO POSITION( FullName-t, "," )
   IF LENGTH( FullName-t ) - pos < 5
   AND POSITION( FullName-t, " " ) < pos
      "FML"
   ELSE
      "LFM"
   END IF
ELSE
   "FML"
END IF

// FullName-t = Last, First Middle
IF RESULT = "LFM"
   ""
   SET pos TO POSITION( FullName-t, "," )
   SET LastName-t TO FIRST( FullName-t, pos - 1 )
   SET FirstName-t TO LAST( FullName-t, LENGTH( FullName-t ) - pos - 1 )
   IF FirstName-t CONTAINS ","
      SET pos TO POSITION( FirstName-t, "," )
      SET Suffix-t TO LAST( FirstName-t, LENGTH( FirstName-t ) - pos - 1 )
      SET FirstName-t TO FIRST( FirstName-t, pos - 1 )
   END IF
   IF FirstName-t CONTAINS " "
      SET pos TO POSITION( FirstName-t, " " )
      SET MiddleName-t TO LAST( FirstName-t, LENGTH( FirstName-t ) - pos )
      SET FirstName-t TO FIRST( FirstName-t, pos - 1 )
      IF MiddleName-t CONTAINS " "
         SET pos TO POSITION( MiddleName-t, " " )
         SET FirstName-t TO FirstName-t + " " + FIRST( MiddleName-t, pos - 1 )
         SET MiddleName-t TO LAST( MiddleName-t, LENGTH( MiddleName-t ) - pos )
      END IF
   END IF

// FullName-t = First Middle Last
ELSE
   ""
   IF FullName-t CONTAINS ","
      SET pos TO POSITION( FullName-t, "," )
      SET Suffix-t TO LAST( FullName-t, LENGTH( FullName-t ) - pos - 1 )
      SET FullName-t TO FIRST( FullName-t, pos - 1 )
   END IF
   SET pos TO POSITION( FullName-t, " " )
   IF pos < 1
      SET pos TO LENGTH( FullName-t ) + 1
   END IF
   SET FirstName-t TO FIRST( FullName-t , pos - 1 )
   SET LastName-t TO LAST( FullName-t , LENGTH( FullName-t ) - pos )
   IF LastName-t CONTAINS "."
      SET pos TO POSITION( LastName-t , "." )
      SET MiddleName-t TO FIRST( LastName-t , pos )
      SET LastName-t TO LAST( LastName-t , LENGTH( LastName-t ) - pos - 1 )
      IF MiddleName-t CONTAINS " "
         SET pos TO POSITION( MiddleName-t , " " )
         SET FirstName-t TO FirstName-t + " " + FIRST( MiddleName-t, pos - 1 )
         SET MiddleName-t TO LAST( MiddleName-t , LENGTH( MiddleName-t ) - pos )
      END IF
   ELSE IF LastName-t CONTAINS " "
      SET pos TO POSITION( LastName-t , " " )
      SET MiddleName-t TO FIRST( LastName-t , pos - 1 )
      SET LastName-t TO LAST( LastName-t , LENGTH( LastName-t ) - pos )
      IF LastName-t CONTAINS " "
         SET pos TO POSITION( LastName-t , " " )
         IF pos < 3 OR pos > 4
            SET FirstName-t TO "«FirstName-t» «MiddleName-t»"
            SET MiddleName-t TO FIRST( LastName-t , pos - 1 )
            SET LastName-t TO LAST( LastName-t , LENGTH( LastName-t ) - pos )
         END IF
      END IF
   END IF
END IF

• Explanation •

This is a moderately sophisticated name parser. It will not recognize prefixes, but it deals fairly well with four and five name elements or more, including a suffix. Note how it parsed the following names:

Sally Johnson  >  Sally : : Johnson
Sally Ann Johnson  >  Sally : Ann : Johnson
Sally Ann P. Johnson  >  Sally Ann : P. : Johnson
Sally Ann de Johnson  >  Sally : Ann : de Johnson
Sally Ann Pauline Johnson  >  Sally Ann : Pauline : Johnson
Sally Ann Pauline de Johnson  >  Sally Ann : Pauline : de Johnson

This computation will work as-is. Simply create the following variables or change them to match your variables:

The computation will work with names in either of the following formats:

First Middle Last
Last, First Middle

To use the computation, simply place the full name in the variable FullName-t, then invoke the computation. After the computation has run, the broken-down elements will be found in the variables FirstName-t MiddleName-t and LastName-t.

The Nitty-Gritty. For names in the format Last, First Middle, the computation can easily determine the last name as everything before the comma. The rest of the name is then inspected for spaces. If a space is found, it will split the name at the space to form the first and middle names. The middle name is then inspected for a space. If a space is found, it is assumed that the first half belongs back with the first name.

The First Middle Last format is more difficult. The name is split at the first space to form the first and last names. We then inspect the last name for a period ".", which presumably will mark the end of the middle initial. If one is found, everything after the period becomes the last name and everything before it the middle name. We should now inspect the middle name for a space. If one is found, there is an extra name which should be shifted from the middle name to the end of the first name.

If there is no period in the last name, then we inspect it for a space. If one exists, the last name will be split into middle and last names. The last name is again inspected for a space. If one is found, a judgment call must be made: does the extra name belong with the last name (as in "Van Winkle") or should it be shifted out of the last name? The decision is based on the length of the first portion of the last name. If it is just two or three characters long (de, dos, Van, Von, etc.) we will assume that it belongs with the last name and leave it as-is. Otherwise, we shift it to the middle name position and append the current middle name to the end of the first name.

This is still an imperfect process, but will produce very good results for most names.


•  •  •  •  •  •  •


• Code •

Simple Parser

""
SET Pos-n TO POSITION( FullName-t, " " )
SET FirstName-t TO FIRST( FullName-t, Pos-n - 1 )
SET LastName-t TO LAST( FullName-t, LENGTH( FullName-t ) - Pos-n )
IF LastName-t CONTAINS( " " )
   SET Pos-n TO POSITION( LastName-t, " " )
   SET MiddleName-t TO FIRST( LastName-t, Pos-n - 1 )
   SET LastName-t TO LAST( LastName-t, LENGTH( LastName-t ) - Pos-n )
END IF

• Explanation •

This computation requires a temporary number variable, Pos-n. (Set it to "Ask only in dialog", "Do not warn if unanswered", and "Do not save in answer file" in the Advanced options).

The first line (after "") tells the computation the position of the first space in the string. It presumes that this space marks the end of the first name.

The second line extracts everything from the beginning of the string up to the space and places it in a text variable, FirstName-t.

The third line extracts everything after the space and places it in a text variable, LastName-t.

The following block repeats the same procedure, this time analyzing LastName-t to see if it has a space in it. If so, it splits this name into MiddleName-t and LastName-t.

Limitation: This does not recognize prefixes or suffixes, and will not work with names longer than three elements.


•  •  •  •  •  •  •

 

• 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