Author Topic: BASIC text parser  (Read 1592 times)

0 Members and 1 Guest are viewing this topic.

Offline EgoTrip

  • 6128 Plus
  • ******
  • Posts: 1.058
  • Country: gl
    • http://egochip.blogspot.co.uk/
  • Liked: 704
  • Likes Given: 304
BASIC text parser
« on: 04: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.
« Last Edit: 04:13, 24 September 13 by EgoTrip »
EgoTrip's Stuff
EgoTrip's Stuff

Offline SuTeKH/Epyteor

  • 6128 Plus
  • ******
  • Posts: 1.394
  • Country: 00
  • Liked: 1308
  • Likes Given: 2049
Re: BASIC text parser
« Reply #1 on: 11: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!)
« Last Edit: 11:54, 24 September 13 by tastefulmrship »

Offline Sykobee (Briggsy)

  • 6128 Plus
  • ******
  • Posts: 833
  • Country: gb
  • Liked: 313
  • Likes Given: 496
Re: BASIC text parser
« Reply #2 on: 13: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...


Code: [Select]

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
« Last Edit: 19:09, 24 September 13 by Sykobee (Briggsy) »

Offline EgoTrip

  • 6128 Plus
  • ******
  • Posts: 1.058
  • Country: gl
    • http://egochip.blogspot.co.uk/
  • Liked: 704
  • Likes Given: 304
Re: BASIC text parser
« Reply #3 on: 17:59, 24 September 13 »
Thanks for the replies, I will have a look at the ACU articles and that example you posted later.
EgoTrip's Stuff
EgoTrip's Stuff

Offline Sykobee (Briggsy)

  • 6128 Plus
  • ******
  • Posts: 833
  • Country: gb
  • Liked: 313
  • Likes Given: 496
Re: BASIC text parser
« Reply #4 on: 18: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.

Offline EgoTrip

  • 6128 Plus
  • ******
  • Posts: 1.058
  • Country: gl
    • http://egochip.blogspot.co.uk/
  • Liked: 704
  • Likes Given: 304
Re: BASIC text parser
« Reply #5 on: 18:52, 24 September 13 »
INPUT$ is not allowed as its a keyword
EgoTrip's Stuff
EgoTrip's Stuff

Offline Sykobee (Briggsy)

  • 6128 Plus
  • ******
  • Posts: 833
  • Country: gb
  • Liked: 313
  • Likes Given: 496
Re: BASIC text parser
« Reply #6 on: 19:11, 24 September 13 »
Changed INPUT$ to WHAT$ :-)
Did CPC BASIC use + in PRINT statements or was it ; ?

Offline EgoTrip

  • 6128 Plus
  • ******
  • Posts: 1.058
  • Country: gl
    • http://egochip.blogspot.co.uk/
  • Liked: 704
  • Likes Given: 304
Re: BASIC text parser
« Reply #7 on: 19: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.
EgoTrip's Stuff
EgoTrip's Stuff

Offline EgoTrip

  • 6128 Plus
  • ******
  • Posts: 1.058
  • Country: gl
    • http://egochip.blogspot.co.uk/
  • Liked: 704
  • Likes Given: 304
Re: BASIC text parser
« Reply #8 on: 20: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.
EgoTrip's Stuff
EgoTrip's Stuff