Question
· May 20, 2022

$zstrip a variable to separate the city, state and zip

I am looking to separate out a variable into 3 other variables

Basically

S REC="CANTON,TX.,75103"

S ZIP= $ZSTRIP(REC,"*","AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz,.")   RETURNS "75103"

S STA=

S CTY=

I am having trouble stripping the other info, any help would be greatly appreciated and I do realize there is a better way to strip the zip, I just haven't found it yet.

 

Thanks in Advance!

Product version: Caché 2017.1
Discussion (15)2
Log in or sign up to continue

so you could try this: 

s REC="A B C,12345"

s REC=$tr(REC," ",",")     ; Translates all Spaces to commas

f x=1:1:$l(REC,",") s Bit=$p(REC,",",x) I Bit?.N s ZIP=Bit      ; This presumes that only one piece of the string is numeric and therefore that's your Zip code. (if there was more than one piece with numerics then it will use the last one) You could be more specific with the pattern match and specify it's ?1.5N (i.e. 1 to 5 Numerics)

Try this one. The idea is, find the state (including the separators), everything before is the city and everything after is the zip code. Then we remove the separator chars (whitespaces, commas and dots).

ClassMethod Disjoin(data, cty, sta, zip)
{
    i $locate(data,"(\s|,|\.)[A-Za-z]{2}(\s|,|\.)",3,,sta) {
        s $lb(cty,zip)=$lfs(data,sta), sta=$$s(sta), cty=$$s(cty), zip=$$s(zip)
        
    } else { s (cty,sta,zip)="" }
    
    q sta]""
    
s(x)	q $zstrip(x,"<>w",",.")
}
Some examples
i ##class(DC.Test).Disjoin("CANTON,TX.,75103",.c,.s,.z) w c,", ",s,", ",z --> CANTON, TX, 75103
i ##class(DC.Test).Disjoin("MILFORD, OH 45150",.c,.s,.z) w c,", ",s,", ",z --> MILFORD, OH, 45150
i ##class(DC.Test).Disjoin("MILFORD OH 45150",.c,.s,.z) w c,", ",s,", ",z --> MILFORD, OH, 45150
i ##class(DC.Test).Disjoin("KANSAS CITY, MO, 12345",.c,.s,.z) w c,", ",s,", ",z --> KANSAS CITY, MO, 12345
i ##class(DC.Test).Disjoin("KANSAS CITY MO, 12345",.c,.s,.z) w c,", ",s,", ",z --> KANSAS CITY, MO, 12345
i ##class(DC.Test).Disjoin("ST. LOUIS MO, 12345",.c,.s,.z) w c,", ",s,", ",z --> ST. LOUIS, MO, 12345
i ##class(DC.Test).Disjoin("  ST. LOUIS MO, 12345",.c,.s,.z) w c,", ",s,", ",z --> ST. LOUIS, MO, 12345

OK, something like this gives a wrong result...
i ##class(DC.Test).Disjoin("   ST. LOUIS MO, 12345",.c,.s,.z) w c,", ",s,", ",z --> , ST, LOUIS MO, 12345

Oh, thanks for the hint, I'm aware of that. Actually one should remove the same characters as used in $locate():

if $locate($zstrip(data,"<w",",."), ...)

but the point is, to circumvent such problems, the rule number one in the electronic data processing is: you have to apply for check each and every input (at least) a formal check or you end up with problems like this. So the desired process should be:

read_data --> check_it --> proceed_if_OK_else_back_to_input

The same goes for data during an import process.

Wesley,

I am a retired Cache/MUMPS developer and have far too much time on my hands.  However, I think I have found a smooth and reliable way to accomplish what you need.  This was fun.  Thanks for the challenge.

CSZ
 New X,CSZ
 Set CSZ($Increment(CSZ(0)))="CANTON,TX.,75103"
 Set CSZ($Increment(CSZ(0)))="MILFORD, OH 45150"
 Set CSZ($Increment(CSZ(0)))="MILFORD OH 45150"
 Set CSZ($Increment(CSZ(0)))="KANSAS CITY, MO, 12345"
 Set CSZ($Increment(CSZ(0)))="KANSAS CITY MO, 12345"
 Set CSZ($Increment(CSZ(0)))="ST. LOUIS MO, 12345"
 Set CSZ($Increment(CSZ(0)))=" ST. LOUIS MO, 12345"
 For X=1:1:CSZ(0) Write $$CSZ2(CSZ(X)),!
 Quit
CSZ2(CSZ)
 New CSZF,SC,CITY,STATE,ZIP
 Set CSZF=CSZ
 Set CSZF=$Zconvert(CSZF,"U")
 Set CSZF=$Translate(CSZF,",."," ") // Translate to spaces
 Set CSZF=$Zstrip(CSZF,"<=>"," ") // Remove Leading, Trailing, and multiple spaces.
 Set SC=$Length(CSZF," ") // Count the number of spaces
 Set CITY=$Piece(CSZF," ",1,(SC-2)) // Select the multiple of city pieces
 Set STATE=$Piece(CSZF," ",(SC-1)) // Select one less the the max piece.
 Set ZIP=$Piece(CSZF," ",SC) // Select the max piece.
 Quit CITY_"|"_STATE_"|"_ZIP
 

VISTA>D CSZ^CSZ
CANTON|TX|75103
MILFORD|OH|45150
MILFORD|OH|45150
KANSAS CITY|MO|12345
KANSAS CITY|MO|12345
ST LOUIS|MO|12345
ST LOUIS|MO|12345