Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Parse Name into separate fields function
Message
From
05/09/2008 12:44:42
Jay Johengen
Altamahaw-Ossipee, North Carolina, United States
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01345118
Message ID:
01345208
Views:
16
>Does anyone have a routine written that they could share to parse a name field into the separate fields.
>
>I have to import a file, and the name is all in one field, the current value of the field is formatted as:
>
>lastname suffix firstname midle-initial
>
>Example
>Kelly Jr Kirk A
>Smith Bob
>Roberts Ben D
>
>
>Thanks for any help.
>
>Kirk

This works for me very well.
******************************************************************************************************************************************
FUNCTION Parse_Name
******************************************************************************************************************************************

	* JJ - Case 00014997 - Function initially created to be used with ParseScheduleNames procedure
	* Routine to parse names into separate name fields.

	LPARAMETERS tcName, taParts, m.NewFirstName, m.NewMidName, m.NewLastName, m.NewSuffix

	* The default input name format is "Prefix First Middle Last Suffix"
	* See end of function for adjustments for other input formats

	* Set variable values used in array
	m.PREFIX = 1
	m.FIRSTNAME = 2
	m.MIDDLENAME = 3
	m.LASTNAME = 4
	m.SUFFIX = 5

	* Define variables and arrays
	LOCAL cPart AS CHARACTER
	LOCAL x , nTotParts AS INTEGER
	LOCAL lIsSuffix , lIsPrefix AS Logical
	LOCAL ARRAY aTemp[1]
	EXTERNAL ARRAY taParts

	* JJ - Case 00016620 - Replace "M.D." with "MD" so name is parsed correctly
	tcName = STRTRAN(UPPER(tcName),'M.D.','MD')

	* Strip periods
	tcName = STRTRAN(tcName,'.',' ')

	* Strip all but the first comma
	m.FirstPart = GETWORDNUM(tcName,1)
	m.FirstPartLength = LEN(m.FirstPart)
	m.RemainingPart = RIGHT(ALLTRIM(tcName),LEN(ALLTRIM(tcName)) - m.FirstPartLength)
	DO WHILE AT(',',m.RemainingPart) > 0
		m.RemainingPart = CHRTRAN(m.RemainingPart,',','')
	ENDDO
	tcName = m.FirstPart + ' ' + m.RemainingPart

	* Strip common name prefixes and suffixes.
	FOR m.LoopSearchStrings = 1 TO 2
		m.ReplaceWith = ''
		m.lcNewName = []
		DO CASE 
			CASE m.LoopSearchStrings = 1
				m.SearchString = [ MR MRS MS MISS DR PROF SIR REV REVERAND FATHER SISTER BROTHER BR SIS ATTORNEY COL COLONOL REP PRES ]
			CASE m.LoopSearchStrings = 2
				m.SearchString = [ PHD II III IV V JR SR ESQ CPA CEA DD MD JD ]
		ENDCASE
		FOR x = 1 TO GETWORDCOUNT( m.SearchString )
		    m.LookFor = ALLTRIM(UPPER(GETWORDNUM(m.SearchString,x))) + [ ]
		    lnAt = AT(m.LookFor,UPPER(tcName) + [ ])
		    DO WHILE lnAt # 0
		       m.lcNewName = LEFT(tcName, lnAt-1) + SUBSTR(m.tcName, lnAt + LEN(m.LookFor))
		       tcName = SUBSTR(m.tcName, lnAt + LEN(m.LookFor))
		       lnAt = AT(m.LookFor,UPPER(tcName) + [ ])

				* JJ - Eligibility Project
				m.NewSuffix = ALLTRIM(m.LookFor)

		   ENDDO
		ENDFOR
		tcName = IIF(EMPTY(m.lcNewName),m.tcName, m.lcNewName)
	ENDFOR

	* Strip multiple spaces
	DO WHILE AT('  ',tcName) > 0
		tcName = STRTRAN(tcName,'  ',' ')
	ENDDO

	* Get the data from tcName into an array for easy looping
	tcName = STRTRAN(ALLT(tcName)," ",CHR(13))
	DIMENSION taParts[5]
	STORE "" TO taParts
	DIMENSION aTemp[1]
	FOR x = 1 TO MEMLINES(tcName)
		cPart = ALLTRIM(MLINE(tcName,x))
		IF !EMPTY(cPart)
			aTemp[ALEN(aTemp,1)] = cPart
			IF x < MEMLINES(tcName)
				DIMENSION aTemp[ALEN(aTemp,1)+1]
			ENDIF
		ENDIF
	NEXT

	* Loop through temp array and put each name segment into it's own place
	nTotParts = ALEN(aTemp,1)
	FOR x = 1 TO nTotParts
		IF VARTYPE(aTemp[x])!="C"
			LOOP
		ENDIF
		* Remove any periods used in the part
		cPart = STRTRAN(ALLTRIM(aTemp[x]),".","")
		IF nTotParts = 1
			* Madonna? Treat single names as a lastname
			taParts[m.LASTNAME] = cPart
		ELSE

			* Probably a better way to handle this, but can't think of one at the momeent and this works
			DO CASE 

				CASE x = nTotParts

					IF EMPTY(taParts[m.LASTNAME])
						taParts[m.LASTNAME] = cPart
					ELSE
						IF EMPTY(taParts[m.FIRSTNAME])
							taParts[m.FIRSTNAME] = cPart
						ELSE
							taParts[m.MIDDLENAME] = cPart
						ENDIF
					ENDIF

				CASE x < nTotParts

					IF EMPTY(taParts[m.FIRSTNAME])
						taParts[m.FIRSTNAME] = cPart
					ELSE
						IF EMPTY(taParts[m.MIDDLENAME])
							taParts[m.MIDDLENAME] = cPart
						ELSE
							taParts[m.LASTNAME] = cPart
						ENDIF
					ENDIF

			ENDCASE

		ENDIF
	NEXT

	* The name is in "Last, First Middle" format and needs to be adjusted.
	IF AT(',', taParts[m.FIRSTNAME]) > 0
		IF EMPTY(taParts[m.MIDDLENAME])
			m.ChangedLASTNAME = taParts[m.FIRSTNAME]
			m.ChangedFIRSTNAME = taParts[m.LASTNAME]
			m.ChangedMIDDLENAME = taParts[m.MIDDLENAME]
		ELSE
			m.ChangedLASTNAME = taParts[m.FIRSTNAME]
			m.ChangedFIRSTNAME = taParts[m.MIDDLENAME]
			m.ChangedMIDDLENAME = taParts[m.LASTNAME]
		ENDIF
		taParts[m.LASTNAME] = m.ChangedLASTNAME
		taParts[m.FIRSTNAME] = m.ChangedFIRSTNAME
		taParts[m.MIDDLENAME] = m.ChangedMIDDLENAME
	ENDIF	

	* Remove comma if it is part of LastName
	taParts[m.LASTNAME] = STRTRAN(taParts[m.LASTNAME],',','')

	* Set the reference variables used for the result name
	m.NewFirstName = taParts[m.FIRSTNAME]
	m.NewMidName = taParts[m.MIDDLENAME]
	m.NewLastName = taParts[m.LASTNAME]

	* Send back a concantenated name in case we ever need it
	RETURN ALLTRIM(ALLTRIM(taParts[m.LASTNAME]) + ;
		IIF(nTotParts = 1 OR (EMPTY(taParts[m.FIRSTNAME]) AND EMPTY(taParts[m.MIDDLENAME])), "", ", ") + ;
		ALLTRIM(taParts[m.FIRSTNAME]) + " " + ;
		ALLTRIM(taParts[m.MIDDLENAME]) )

ENDFUNC
******************************************************************************************************************************************
Previous
Reply
Map
View

Click here to load this message in the networking platform