TIP: Click on subject to list as thread! ANSI
echo: power_bas
to: ALL
from: BRIAN MCLAUGHLIN
date: 1995-11-13 19:19:00
subject: Parsing function

Here's some PowerBASIC 3.x code written by James Davis that everyone
will find useful sooner or later. It's a function for parsing a string
into tokens, based on a set of delimiters you supply. For those who are
new to the term, a "token" a just a sub-string, that is set off by
delimiting characters.
For example, in a normal text file, the words can be set apart by a
large number of delimiters, like spaces, commas, periods, parentheses,
tabs and other punctuation. You could use this function to break out
just the words in the text, one by one, by identifying all the possible
punctuation and parsing it out. In this case the words become the
"tokens".
Or to cite an even more popular use: you can use this function to help
you parse a command line into separate arguments or parameters. Again,
each parameter is a token, and you say what the delimiters are.
Just one warning: this function destroys the string you send it, so
make a copy. Here it is, with some example code attached:
'---------------------  START CODE ----------------------------
$LIB ALL OFF
DECLARE FUNCTION Parse$ (Temp$, Delimiter$)
Test$ = "This string has: a colon, commas, some words, and a period."
Delim$ =" ,.:"
CLS
PRINT "Before parsing, Test$ = "; Test$
PRINT
Tok$ = Parse$(Test$, Delim$)
DO WHILE LEN(Tok$)
   INCR Count%
   PRINT Tok$
   Tok$ = Parse$(Test$, Delim$)
LOOP
PRINT
PRINT "Test$ had"; Count%; "tokens."
PRINT "After parsing, Test$ = "; Test$
END
'====================================================================
 FUNCTION Parse$(Temp$, Delim$)
'====================================================================
' Original code by James Davis.  Caution: This function changes Temp$!!
'
' This function parses out the first token in Temp$, as defined
' by the delimiters in Delim$, then it removes that token from Temp$
' and sends the token back to the caller as its return value. By calling
' it repeatedly in a loop, it will parse out all the tokens in Temp$.
  Temp$ = LTRIM$(RTRIM$(Temp$), ANY Delim$)
  Tmp$ = LEFT$(Temp$, 1) + EXTRACT$(MID$(Temp$, 2), ANY Delim$)
  IF LEN(Tmp$) < LEN(Temp$) THEN
    Temp$ = MID$(Temp$, LEN(Tmp$) + 1)
   ELSE
    Temp$ = ""
  END IF
  Parse$ = Tmp$
END FUNCTION
'------------------------  END CODE --------------------------
 * SLMR 2.1a * MAXLIB For PB v1.2 - Access arrays and files in EMS/XMS!
--- WILDMAIL!/WC v4.12 
---------------
* Origin: Com-Dat BBS - Hillsboro, OR. HST DS (1:105/314.0)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.