CPCWiki forum

General Category => Programming => Topic started by: EgoTrip on 02:09, 24 September 13

Title: BASIC text parser
Post by: EgoTrip on 02:09, 24 September 13
I'm doing something out of boredom in BASIC, and am wondering how to parse text.

I have a command prompt, LINE INPUT a$. How do i get it to split up the text into variables, and remove (ignore) any preceeding and following spaces. For example if the user typed in "help commands" into a$ it would split up the 2 words into b$ and c$ without any spaces (and d$,e$ etc for commands that have even more parameters - I am also using LOWER$ for various reasons). I will probably have more questions too as the project gets more complex and if I don't abandon it.
Title: Re: BASIC text parser
Post by: tastefulmrship on 09:24, 24 September 13
ACU had a great feature on writing adventure games that used 2 word commands (April1984-September1984).
However, like all parsers, it's specific to the task and any 3 word commands failed; the ended up as cmd1$="GO":cmd2$="FUCK YOURSELF" and so failed the command check later into the program.





(Hey! Don't you guys tell me you never swore in an adventure game! WE ALL DID IT! You're just trying to convince yourself if you deny it!)
Title: Re: BASIC text parser
Post by: Sykobee (Briggsy) on 11:37, 24 September 13
There was also a later series on adventure writing in ACU that might have been more flexible, and I think there was one in CWTA as well.


But in effect you will want to split the input up into words at the space boundaries. Which requires you to loop through the string...


untested, I've totally forgotten CPC BASIC...




10 DIM WORD$(4):REM Number of words we care to parse e.g., GET SMALL GOLD KEY; SAY HI TO BOB;
20 I%=0
25 INPUT "What now? ", WHAT$
30 FOR J% = 0 TO LEN(WHAT$)
40 WORD$[I%]=WORD$[I%]+MID$(WHAT$, J%, 1)
50 IF MID$(WHAT$, J%, 2)=" " THEN I%=I%+1
60 IF I% = 5 THEN GOTO 80
70 NEXT J
80 FOR J%=0 TO I%-1
90 PRINT "Word " + J% + " IS " + WORD$[J%]
100 NEXT
Title: Re: BASIC text parser
Post by: EgoTrip on 15:59, 24 September 13
Thanks for the replies, I will have a look at the ACU articles and that example you posted later.
Title: Re: BASIC text parser
Post by: Sykobee (Briggsy) on 16:10, 24 September 13
I wanted to test it in cpc-box but it seems to be set to a french keyboard layout or something and I haven't got the time to work out why. The settings say british, but it's azerty.
Title: Re: BASIC text parser
Post by: EgoTrip on 16:52, 24 September 13
INPUT$ is not allowed as its a keyword
Title: Re: BASIC text parser
Post by: Sykobee (Briggsy) on 17:11, 24 September 13
Changed INPUT$ to WHAT$ :-)
Did CPC BASIC use + in PRINT statements or was it ; ?
Title: Re: BASIC text parser
Post by: EgoTrip on 17:43, 24 September 13
You can use both, + is used on the same line, ie PRINT a$+b$. You can use PRINT a$;b$ which has the same effect, but the advantage to ; is if you have PRINT a$;b$; the next thing to be PRINTed in the program (assuming no LOCATE or similar) will immediately follow it.
Title: Re: BASIC text parser
Post by: EgoTrip on 18:00, 24 September 13
Reading the manual, i think INSTR might be the way to go with this. Making a bit of progress, will post what I get working if I don't need to ask for more help.
Powered by SMFPacks Menu Editor Mod