News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_EgoTrip

BASIC text parser

Started by EgoTrip, 02:09, 24 September 13

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

EgoTrip

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.

tastefulmrship

#1
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!)

Sykobee (Briggsy)

#2
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

EgoTrip

Thanks for the replies, I will have a look at the ACU articles and that example you posted later.

Sykobee (Briggsy)

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.

EgoTrip

INPUT$ is not allowed as its a keyword

Sykobee (Briggsy)

Changed INPUT$ to WHAT$ :-)
Did CPC BASIC use + in PRINT statements or was it ; ?

EgoTrip

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.

EgoTrip

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