Hi Ken;
I like your approach and suggested a very similar way to Brian
quite a while ago complete with a working example. He has chosen to
stick with this method so my recent suggestions have shown how to use
INSTR(), LEFT$(), and MID$() to parse his WIN instruction.
Learning a language goes in stages; perhaps working with arrays
will be next. Here is that example using an array again. It parses
the entire command variable before the SELECT/CASE and each CASE just
uses the items from the array.
HERE'S A SAMPLE PROGRAM TO DEMONSTRATE PARSING A COMMAND
INTO AN ARRAY or LIST OF WORDS. That list can then be used easily
to carry out the intended instruction each time This demo justs
prints the items in the list to show the concept.
STUDY IT. PLAY WITH IT. USE IT. OR ADAPT IT, AS YOU SEE FIT.
If you use it, place the 2 subroutines and the global definition at
the start of your program so they are defined before being used.
HTH
'======================================================================================
// DEMO PROGRAM FOR BRIAN H - PARSING COMMAND LINES
// INTO A LIST OF WORDS
// FN Digest - October 10, 2004 By Stu Cram, OGSS
_maxNumWords = 20
' used for size of gWords array, change to more if needed.
DIM gWord$( _maxNumWords )
END GLOBALS
LOCAL FN TRIM$( txt$ )
' This function deletes any leading and trailing spaces from txt$
DIM result$
result$ = txt$
DEF Truncate( result$ )
' remove trailing spaces
WHILE ( LEN(result$) > 0 ) AND ( result$[1] = _" " )
' remove leading spaces
result$ = mid$( result$, 2 )
WEND
END FN = result$
LOCAL FN ParseCommand( cmdLine$ )
' This procedure breaks a text list with comma separators and
' puts the individual words in global array gWord$
' Leading and trailing spaces are removed. (See FN TRIM$)
' The leading word should be the command keyword and end with
' a space, not a command. It goes in cell 0 of the gWord$ array.
' Missing items become null strings.
' Quotation marks " are not handled here. Better not to use them.
'
' Ex. XXXX WON, TOO, TREE, FORE ,PHYVE, SICKS ,. ATE, K 9 , Etc.
' Will give these results in the global array gWord$
' gWord$(0) <--- "XXXX" (keyword like PRINT, WIN, or CLEAR)
' gWord$(1) <--- "WON"
' gWord$(2) <--- "TOO"
' gWord$(3) <--- "TREE"
' gWord$(4) <--- "FORE"
' gWord$(5) <--- "PHYVE"
' gWord$(6) <--- "SICKS"
' gWord$(7) <--- ""
' gWord$(8) <--- "ATE"
' gWord$(9) <--- "K 9"
' gWord$(10) <--- "Etc."
' gWord$(11) <--- ""
' ...
' gWord$(20) <--- ""
'
DIM wordNum, x ' counters
DIM wrd$ ' single word or pharase being built
DIM 1 ch$ ' single character from line
DIM 1 delim$ ' SPACE (after keyword) or COMMA (after parameter)
'
' Erase all values in the array
FOR wordNum = 0 TO _maxNumWords
gWord$( wordNum ) = ""
NEXT wordNum
'
' Examine command line char by char to get the words between commas
wordNum = -1
cmdLine$ = FN TRIM$( cmdLine$ )
' remove any leading or trailing spaces
cmdLine$ = cmdLine$ + ","
' ensure it has a comma after last item
wrd$ = ""
delim$ = " "
' delimeter is space after starting keyword
FOR x = 1 to LEN( cmdLine$ )
ch$ = MID$( cmdLine$, x, 1 )
' get a single char at position x in command line
LONG IF ch$ <> delim$
' Is it a comma (or space after keyword)
wrd$ = wrd$ + ch$
' No - add character to end of word
XELSE
wordNum ++
' Yes - increase word count
IF wordNum > _maxNumWords THEN wordNum = _maxNumWords
' prevent array overflow
gWord$( wordNum ) = FN Trim$( wrd$ )
' store the word in the array
wrd$ = ""
' reset word to null string
delim$ = ","
' change delimeter to comma for remaining words
END IF
NEXT x
gWord$(0) = UCASE$( gWord$(0) )
' capitalize the keyword at start of the instruction
END FN
'~' Main Program ...
DIM SampleLineStr$
DATA "xx-Demo-xx WON, TOO, TREE, FORE ,PHYVE, SICKS ,, ATE"
DATA "WIN 7,Help Me,100,200,300,400,5"
DATA "Menu 3,COLORS,RED,Blue,Green,,Yellow,Pale Blue,Pink, ,Black,White"
DATA "ShowURL http:gym-Score-Depot.ca"
DATA "*** STOP DEMO ***"
WINDOW 1, "Parsing CommandLines Demo", (0,0)-(600,600), 5
DO
CLS
READ SampleLineStr$
IF SampleLineStr$ = "*** STOP DEMO ***" THEN END
FN ParseCommand( sampleLineStr$ )
PRINT
TEXT _Courier,14,1 : COLOR _zBlack
PRINT : PRINT sampleLineStr$
TEXT _Courier,12,0
PRINT
FOR n = 0 TO _maxNumWords
LONG COLOR 32000,32000,32000
' gray
PRINT USING "### " ; n ; "<" ;
IF n > 0 THEN COLOR _zBlue ELSE COLOR _zRed
' blue for parameters, red for keyword
PRINT gWord$( n ) ;
LONG COLOR 32000,32000,32000
' gray
PRINT ">"
NEXT n
TEXT _Geneva, 9, 0 : Color _Magenta
PRINT : INPUT "-----> Press 'RETURN' to continue..."; n
UNTIL 0
'======================================================================================
--
- Stu Cram, OGSS
http://Gym-Score-Depot.ca
--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@...