Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing names within a field

Status
Not open for further replies.

youwannawhat

Programmer
Oct 3, 2001
43
0
0
US
I have a table that needs its structure and contents reformulated. The 'NAME' field currently contains entries like 'JOHNSON JOSEPH T', or 'SMITH BILL'. I need to parse these into separate fields for last, first and middle names.

What's the easiest way to do this?

Thanks,

Paul
 
Not sure if this is the easiest way, but it was quick for me... if you're working with at least version 7 - check out the Alines() function

ALINES(ArrayName, cExpression [, lTrim] [cParseChar, ,… ,cParseChar])


then you can loop through the array and insert each index into the correct field
 
Paul,

If you are using VFP 7.0 or later, you could try the new GETWORDNUM() function. That'll separate out the separate components of the name. Of course, it won't deal with cases where a name contains two distinct words, but then neither will any other programmatic method.

Mike


Mike Lewis
Edinburgh, Scotland
 
I like to "roll my own" just for the exercise ....

Here is something that would probably work ...


use datafile
go top
do while not eof()
cData = alltrim(NAME)
nLen = len(cData)
cSpace = [ ]

cLastname = []
cMiddlename = []
cFirstname = []

nItem = 0
cItem = []
for j = 1 to nLen
ch = substr(cData,j,1)
if (ch = cSpace) or (j = nLen)
nItem = nItem + 1
do case
case nItem = 1
cLastname = cItem
case nItem = 2
cFirstname = cItem
case nItem = 3
cMiddlename = cItem
otherwise
endcase
else
cItem = cItem + ch
endif
endfor

* Process cFirstname, cMiddlename, cLastname here

* Go to next record
skip
enddo


Don
dond@csrinc.com

 
that's essentially what the alines function does, it separates strings out by a delimiter, why bother reinventing the wheel?
 
The GETWORDNUM() function seems to work perfectly for my needs within a scan loop. Thanks again, everybody.

 
Kenndot:

"why bother reinventing the wheel"

You are right, of course.

I just am not aware of some of the tools available so I just roll my own.

But I am now aware of Alines().

Thanks.


Don
dond@csrinc.com

 
Rolling your own is also the only way to account for JRs, SRs, and other such occurances. Yes, programming for all possibilities is long and endless. In the end you have to decided the time/economic trade offs. IF you do enough rolling (tweaking)of your rough wheel it will wind up giving a smoother ride(more satisfactory results) than any built in function. The same thought process applies to parsing addresses as well.
 
I agree with jjtaylor... I've developed a routine that isolates prefixes (Mr, Mrs, Dr, Rev, etc.) and suffixes (Jr, Sr, III, Esq, CPA, etc.) and then determines how many words are left. From the resulting string the first 'word' is presumed to be the first name, the last 'word' is presumed to be the last name, and everything else is lumped into the middle name. Of course I'm trapping for only one word in the resulting string and setting it to the last name and then exiting.

Steve
 
Also see thread184-567614. If you want the Walt Kennamer code (with sample data) just post an address.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top