[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Pattern-matching...
CdrJeanne4 writes ...
>
> Hello, fellow Apple enthusiasts!
>
> I'm a high school student who has only a bit of experience with Applesoft BASIC
> (but not too much), and I have a question about something. I know that you can
> match character strings with one another (e.g IF $A="apple" THEN GOTO 50; basic
> stuff like that). But can you match wildcard characters, like in other
> languages? Is there any way to match, for example, the user's input "My name is
> Laura " with "my name is "; or "Hello, the sky is blue" with "hello", and then
> provide output based on that match? Can this be done with the READ and DATA
> commands (which, I'm ashamed to admit, I don't really understand too well)?
This sort of challenge comes up in Text adventure game programs where you
want to get the main idea of a player's input. For instance, if "HELLO" shows up,
you want your program to respond whether the input is HELLO, THERE! or HELLO. or
SAY "HELLO" or just HELLO . The game 'flows' better and the game program looks a
lot smarter if it makes an appropriate response to the player's message than if it
comes back with ? or I DO NOT UNDERSTAND because the player added a superflurous
word or punctuation mark.
The usual way to extract meaning from an input is to look for matches to
words your program is supposed to recognize. The way you pick out words is,
usually, to look for letter groupings not interupted by spaces or punctuation.
Here is a sample program which uses a routine for picking out and saving up
to seven words ...
10 GOTO 100
29 REM **GET WORDS SUB
30 INPUT "> ";I$: IF I$ = "" THEN 30
32 FOR I = 1 TO 7:E$(I) = "": NEXT I
34 L = LEN (I$)
36 Z = 1:K = 1
38 W = 0: FOR I = Z TO L
40 Q$ = MID$ (I$,I,1)
42 A = ASC (Q$)
44 IF A > 64 AND A < 91 THEN E$(K) = E$(K) + Q$:W = 1: NEXT I
46 K = K + W: IF K > 7 THEN 50
48 Z = I + 1: IF Z < = L THEN 38
50 K = K - 1: RETURN
99 REM *** BEGIN MAIN LINE ***
100 TEXT : HOME
110 DIM E$(7)
120 H = 0
130 GOSUB 30
140 PRINT : FOR I = 1 TO K: PRINT E$(I): IF E$(I) = "HELLO"
OR E$(I) = "HI" THEN H = 1
150 NEXT I: PRINT
160 IF H THEN H = 0: PRINT : PRINT "HELLO, JEANNE!": PRINT
170 IF E$(1) < > "STOP" THEN 130
900 END
What the subroutine which begins at Line 30 does is to collect groups of letters
(actually, capital letters) and put each group in the E$() array.
Letters are detected in line 44. At the beginning, you are looking at the first
character in the input string I$. If it is a letter it gets added to E$(K)-- which
is E$(1) at the start-- and W is set to 1 to indicate that a Word has been found.
So long as you keep getting letters, the E$(K) string gets added to. If you run
out of characters, the NEXT I ends the loop and you go to Line 46.
Otherwise, once any non-letter occurs you fall out of the loop to Line 46. Since
W= 1, K is incremented by 1 (to set the E$() index for the next word) and there is
a test to exit if you now have 7 words.
If you do not have 7 words you will go to Line 48. Z is the starting point for the
FOR-NEXT loop in Line 38. It tells you which character in I$ to look at first each
time you look for a word. Z starts = 1 as set in Line 36.
In Line 48 Z is set to I + 1 because, supposedly, the Ith character was a
non-letter which terminated the last word found. You do not need to look at it
again; so, you set Z to 'point to' the next character which you have not seen yet.
The value of Z is checked to make sure it is not pointing past the end of the
string. If it is not, you hop back to Line 38, re-set W (your 'Word Found'
indicator) to 0 and start the loop again with a look at the Zth character in I$.
The second time you go through the loop and find at least one letter, the Word
Found indicator will be set again and the group of one or more letters will be
saved in E$(2).
Once you run out of characters or you have found 7 words, the subroutine sets K=
to the number of words found-- handy to know-- and exits via the RETURN in Line
50.
One nice feature of the subroutine is that it does not get fooled by successive
non-letter characters into using up E$() slots. So, if the user types in something
like MY NAME is PAUL , even with leading and trailing spaces, the
routine will exit with
E$(1)= "MY"
E$(2)= "NAME"
E$(3)= "IS"
E$(3)= "PAUL"
and K= 4 to let you know there are 4 words to deal with.
If you wanted to let users enter numbers as well as letters, you would change Line
44 to include the ASCII values for number characters 0-9 ...
44 IF (A > 64 AND A < 91) OR (A > 47 AND A < 58)
THEN E$(K) = E$(K) + Q$:W = 1: NEXT I
The Main Line of the program includes a couple examples of using the
information typed in to make things happen:
1. If you type in HELLO or HI, the program will set H=1 and respond with HELLO
JEANNE!
(If the program knew the user's name via an earlier entry which saved it as N$,
Line 160 could be changed to ...
160 IF H THEN H = 0: PRINT : PRINT "HELLO, "N$"!": PRINT
and it could respond HELLO, PAUL! etc. depending upon N$.)
2. If you the first word you type in is STOP, the program will wind up going to
Line 900 and executing the END command.
As you can see, getting words from inputs is fairly easy. Once you have
words, like HELLO or NAME (or GO SOUTH or LOOK or ...) your program can make all
sorts of decisions.
> And
> can anyone direct me to some good references on Applesoft BASIC? It would all
> be most appreciated.
Apple II Lessons & Software- BASIC Lessons and A2 software
http://www.iglou.com/qwerty/kb/dlfiles.html
Apple II Programmer's Archive- language software
http://net-24-42.dhcp.mcw.edu/a2pa.html
Some good books include ...
Basic Programming Reference Manual from Apple
ProDOS Inside and Out by Doms and Weishaar
The DOS Manual from Apple
Apple II User's Guide by Poole, Martin, and Cook
Beagle Bros "Peeks, Pokes, and Pointers" (poster) by Beagle Bros
A good source of other Apple II information is the newsgroup FAQs. It includes a
good listing of Apple II software and information sites. You can find the FAQs at
...
Home site- for downloading or Viewing via an FTP program ...
ftp://ground.ecn.uiowa.edu/apple2/Faqs/
Mirrors- for on-line perusing via Netscape, etc. ...
http://www.grin.net/~cturley/A2.FAQs.and.INFO/CSA2.FAQs/
ftp://apple.cabi.net/pub/applegs/FAQs.and.INFO/A2.Csa2.FAQs/
There are, also, a couple in-progress HTML versions of the FAQs Q&A files ...
http://members.xoom.com/apple_II/faqs.html
email: ron@pacifier.com
http://www.grin.net/~cturley/A2.FAQs.and.INFO/CSA2.FAQs.HTML.folder/
email: cturley2@aol.com
And, a super-good utility for Apple II BASIC programming is Program Writer.
> I'm sorry if this is one of those really idiotic questions that nobody will
> even deign to answer...but I've had this on my mind for some time.
....
It's a good question.
> But I've recently developed an
> affinity for this sand-colored hunk of plastic sitting in the garage. It's even
> kinda cute, in a way.
Yeah, it is sort of a neat computer!
Rubywand