Skip to content

Instantly share code, notes, and snippets.

@CarlTBarnes
Created July 5, 2020 22:39
Show Gist options
  • Save CarlTBarnes/709d454636f2d441072fb40714c5138e to your computer and use it in GitHub Desktop.
Save CarlTBarnes/709d454636f2d441072fb40714c5138e to your computer and use it in GitHub Desktop.
Parse String with Delimited with Tokens into a Queue
MAP
ParseTokenStringIntoQueue(CONST *STRING pString,STRING pToken,*QUEUE OutQueue,BYTE pBlanksOk=0),LONG,PROC !Return Count
END
ParseTokenStringIntoQueue PROCEDURE(CONST *STRING pString,STRING pToken,*QUEUE OutQueue,BYTE pBlanksOk=0)!,LONG,PROC !Return Count
SLen LONG,AUTO !pString Length
Ndx LONG,AUTO
TokenVal BYTE,AUTO !When hunting for chars I like VAL compares
BegPos LONG(1) !BegPos = 1 required to work right
EndPos LONG,AUTO !EndPos = 0 required to work right
ReturnCnt LONG(0)
CODE ! Begin processed code
SLen=LEN(pString)
TokenVal=VAL(pToken)
LOOP Ndx = 1 to SLen + 1 !Go 1 past end of string and assume end is final separator
IF Ndx <= SLen AND VAL(pString[Ndx]) <> TokenVal THEN CYCLE. !Not a separator, and < end of pString cycle back for more
IF Ndx>BegPos OR TokenVal<>32 THEN !remove double spaces
EndPos = Ndx !Save end position of the last separator
IF EndPos > BegPos THEN !Endpos only set if parm found, If sequential separators then End-1<Beg and so slice is not valid, so fall thru to return empty
OutQueue = pString[ BegPos : EndPos - 1 ]
ELSE ! double tokens
OutQueue = '' !if Queue has add'l fields that are not string this could be problem
END
IF pBlanksOk OR OutQueue THEN
ADD(OutQueue)
ReturnCnt += 1
END
END
BegPos = Ndx + 1 !New data begins after previous pToken
END
RETURN ReturnCnt
!---------------------------- Example -------------------
ParQ QUEUE
S STRING(32)
END
BarData STRING('123|456||789')
Cn LONG
CODE
Cn=ParseTokenStringIntoQueue(BarData,'|',ParQ)
Message('Count=' & Cn &' Records(ParQ)=' & Records(ParQ) &' Last ParQ=' & ParQ)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment