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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string parsing with comma 1

Status
Not open for further replies.

angelleynes

Programmer
Dec 28, 2001
46
US
Hello,

I need help on how to split strings?
example: "PAYOFF DEPARTMENT, 8001 N. Stemmons Fwy, Dallas, TX 75247"

what I want to do is to split them so that "PAYOFF DEPARTMENT" being the name, "8001 N. Stemmons Fwy" being the address, "Dallas" being the city, "TX" being the state and "75247" being the zip.

it would be nice to count for how many commas in the string and use it to split the string and when to print the next string..

thanks in advance!
 
Here is a generic function I use for this purpose, I realized that the Zip code id not separated by comma from the state, but it will be easy to extract it from the last element of the array this function returns.

*////////////////////////////////////////////////////////////////////////////////////////////////////////////
*// Author : IIF(This Code Is Working Ok, Walid Magd, I Have No Idea) :)
*//
*// Parameters : 1) A string consists of some words separated by delimiter in
*// the first parameter lcString.
*// 2) Array by reference in the second parameter
*// laArray.
*// 3) Optional. Specifies the character used to delimit character groups in lcString.
*//
*// Functionality : 1) Detect if any specific delimiting character was passed in the 3rd parameter
*// If not, this function by default assumes that words are delimited by spaces or tabs
*// 2) Parsing lcString to detect How many words in this string. This number will be used
*// to assign the right dimension for the array.
*// 3) Extract every word and assign it to new array element.
*//
*// Returns : True if the array successfully Re dimensioned and populated with the words in the string.
*// : False if any error encountered
*//////////////////////////////////////////////////////////////////////////////////////////////////////////
FUNCTION String2Array
Parameters lcString, laArray, lcDelimiter
Local lcMessageText,lnMessageIcon,lcMessageTitle,lcOldError,lnAlen,llError
lcMessageTitle = "Error.."
lnMessageIcon = 0+16+0
*// Parameter checking procedure [Start]
Do CASE
lcMessageText = Space(0)
Case PCOUNT() < 2
lcMessageText = &quot;Too few parameters&quot;
Case PCOUNT() > 3
lcMessageText = &quot;Too many parameters&quot;
Case EMPTY(lcString)
lcMessageText = &quot;Can't parse an empty string!&quot;
Case PCOUNT() = 3 AND VARTYPE(lcDelimiter) != 'C'
lcMessageText = &quot;The delimiter must be of type character!&quot;
CASE PCOUNT() = 3 AND LEN(ALLTRIM(lcDelimiter)) > 1
lcMessageText = &quot;The delimiter must be only one character!&quot;
Endcase

If !Empty(lcMessageText)
MessageBox(lcMessageText,lnMessageIcon,lcMessageTitle)
Return .F.
Endif
*// Make sure that laArray is variable of type Array [Start]
lcOldError = ON('ERROR')
llError = .F.
ON ERROR llError = .T.
lnAlen = ALEN(laArray)
IF llError
lcMessageText = &quot;Can't manipulate the array, make sure that it defined properly and passed by reference!&quot;
MessageBox(lcMessageText,lnMessageIcon,lcMessageTitle)
ON ERROR &lcOldError
Return .F.
Endif
*// Make sure that laArray is variable of type Array [End ]

*// Free memory
RELEASE lcMessageText,lnMessageIcon,lcMessageTitle,lcOldError,lnAlen,llError
ON ERROR &lcOldError
*// Parameter checking procedure [End ]

Local llBuiltInFunction, llSetLibrary, lnWordCount, lcExpWordCount, lcExpWordNum, lcOldLib
Store SPACE(0) TO lcExpWordCount, lcExpWordNum, lcOldLib
llSetLibrary = .F.
lnWordCount = 0
lnIndex = 0
llBuiltInFunction = IIF(Left(Version(4),2) == &quot;07&quot;,.T.,.F. )
lcDelimiter = IIF(PCOUNT()=2 OR EMPTY(lcDelimiter),SPACE(0),ALLTRIM(lcDelimiter))

If !llBuiltInFunction
If !(&quot;FOXTOOLS&quot; $ UPPER(SET('LIBRARY')))
lcOldLib = SET(&quot;LIBRARY&quot;)
*//Make sure FoxTools.fll in the root directory of your application b4 issuing this command.
Set LIBRARY TO FOXTOOLS.FLL ADDI
llSetLibrary = .T.
Endif
Endif
lcExpWordCount = IIF(llBuiltInFunction,[GETWORDCOUNT(],[Words(])



lcExpWordCount = lcExpWordCount + &quot;lcString&quot;+IIF(EMPTY(lcDelimiter),[)],[,+lcDelimiter)] )

lcExpWordNum = IIF(llBuiltInFunction,[GETWORDNUM(],[WordNum(])
lcExpWordNum = lcExpWordNum + &quot;lcString, lnIndex&quot;+IIF(EMPTY(lcDelimiter),[)],[,+lcDelimiter)] )


lnWordCount = EVAL(lcExpWordCount)
If lnWordCount > 0
Dimension laArray[lnWordCount] && Redimensioning the array to the right number
For lnIndex = 1 TO lnWordCount

laArray[lnIndex] = EVAL(lcExpWordNum)
Endfor
Endif


*// Restore the old sitting b4 you go
If llSetLibrary
Set LIBRARY TO &lcOldLib
Endif

Use Example...
DIMENSION la[1]
lcString = &quot;Walid Magd&quot;
=string2array(lcString,@la,&quot;,&quot;)
This will return only one element la[1]=&quot;Walid Magd&quot;
=string2array(lcString,@la)
This will return two elements la[1]=&quot;Walid&quot;
la[2]=&quot;Magd&quot;

Hint: You can extract the Zip from the last element of the array by calling the function again with the last element of the array as string and no 3rd parameter.

Good luck



Walid Magd
Engwam@Hotmail.com
 

local parsethis
parsethis = &quot;PAYOFF DEPARTMENT, 8001 N. Stemmons Fwy, Dallas, TX 75247&quot;

firstc = atc(&quot;,&quot;,parsethis,1) - 1
secondc = atc(&quot;,&quot;,parsethis,2) - 1
thirdc = atc(&quot;,&quot;,parsethis,3)

nName = allt(substr(parsethis,1,firstc))
? NName
addr = allt(substr(parsethis, firstc + 3,secondc - firstc - 2))
?addr
city = allt(substr(parsethis, secondc + 3,thirdc - secondc - 3))
? city
state = allt(substr(parsethis, thirdc +1))
? state
 
thanks shangrilla,

what if there's an extra comma like the following:
&quot;PAYOFF DEPARTMENT, Attn: Angel L., 8001 N. Stemmons Fwy, Dallas, TX 75247&quot;

and I want &quot;Angel L.&quot; to be nName2, what adjustments do i need?

thanks in advance!

 
there have been some great dieas for you to work with. I will pass on some experance I had with a major east coast city police department. I had to convert their old main frame system to SQL server 6.5. In processing their data i have discovered that their old system had no data rules or formating. this meant that the data was a mess. they had to make some commitments to following some converting guide lines. So if you run into an odd address format you may have some odd data in fields. the way this city looked at it was that they did not want to loose anything. if it showed up in the wrong filed they could live with this and fix in an as case bases.

Attitude is Everything
 
In VFP6 we got a really neat function &quot;ALINES&quot; which does this nicely:

Code:
LOCAL lnCnt, laFlds[1]
StringToParse = &quot;PAYOFF DEPARTMENT, 8001 N. Stemmons Fwy, Dallas, TX 75247&quot;
lnCnt = ALINES( laFlds, StrTran( StringToParse, &quot;,&quot;, Chr(13)+chr(10) )

Your fields are now in the array &quot;laFlds&quot;.
( Notice that I used Chr(13)+chr(10) instead of just Chr(13)... if you use only chr(13) (cr), then empty spots are ignored; if you use CrLf, then empty spots are preserved as blanks in the array)
 
angelleynes

My solution was limited to your String posted here. Assuming that they will always do something like &quot;Name, Attn: Angel L., Add, City, State&quot; the following should work:

local parsethis
parsethis = &quot;PAYOFF DEPARTMENT, Attn: Angel L., 8001 N. Stemmons Fwy, Dallas, TX 75247&quot;
firstc = atc(&quot;,&quot;,parsethis,1) - 1
secondc = atc(&quot;,&quot;,parsethis,2) - 1
thirdc = atc(&quot;,&quot;,parsethis,3) - 1
fourthc = atc(&quot;,&quot;,parsethis,4)

nName = allt(substr(parsethis,1,firstc))

*In this case you'll search for the word 'Attn':
if like('Attn*', allt(substr(parsethis, firstc + 3,secondc - firstc - 2))) then
*newName =
newName = nName + ' '+ allt(substr(parsethis, firstc + 9,secondc - firstc - 8)) &&'extract the value from the above string'
?newName
endif

addr = allt(substr(parsethis, secondc + 3,thirdc - secondc - 3))
?addr
city = allt(substr(parsethis, thirdc + 3,fourthc - thirdc - 3))
? city
state = allt(substr(parsethis, fourthc +1))
? state

Not the best way of doing things, but hey it still works.
 
Nice wgcs. I've been trying to think of something like it for a little while now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top