Lightning-fast command finder
By Kevin Sharratt
Two powerful and widlely useful routines for the ACTION! programming language. These programs work on all 8-bit Atari computers of any memory size, with disk or cassette. The ACTION! cartridge from ICD/OSS is required.
Whether you're using ACTION! to build "The Wizards of Zondar" or "The Ultimate Chef's Companion," your programming toolbox will be incomplete without a procedure that removes individual words from a string you've entered--and a procedure that compares those words with a list of known words in hopes of a match.
For efficiency and versatility, the following two procedures fill the gap nicely and can easily be customized by experienced ACTION! programmers.
This procedure strips each Word, one at a time, from String--which is a global BYTE ARRAY similar to a BASIC string variable. In the process it discards the spaces between Words, no matter how many times you pressed the [SPACEBAR].
In its first loop, Wordfind() searches String for a non-space character, incrementing the Index into the array as it goes. Upon finding one, it stores the Index value in Start. The next loop searches for a space--and the end of the Word -while continuing to increment Index.
When another space or the end of the array is found, the procedure writes the characters between Start and Index into the global Word. Since Index, too, is a global variable, calling Wordfind() again will result in the next consecutive Word. Therefore, Index must be set to 1 before each new string is examined.
In most applications, after you isolate a single Word you'll want to check it against the commands with which your program is prepared to deal. Matchup() can help you here.
This procedure requires that each global List of commands contains only elements of the same length. For example:
Comlist1='EAST WEST NORTH SOUTH".
Comlist2 = 'EAWENOSO"
In Comlist1 the Increment is five--meaning that a new command begins every five characters. Comlist2 has shortened those same commands to two characters. In either case, Matchup() must be called using three parameters: the potential Command to be compared, the List of known commands and the Increment of the list.
Matchup() then jumps through the List by Increments, searching the first character of each command for a match. Upon finding one, it compares the remaining characters. If all the characters match, it alters the global variable Match to show where in the list the command was found. For example, after calling Matchup(Word,Comlist1 ,5) you find that Match=6. You then know that "Word" matched the command beginning at character 6--in this case, WEST.
Matchup() will not search past either the given Increment or a space. Thus, if you call it to examine the word WESTERLY against Comlist1, Match would still equal 6. If no match is found, Match will equal 0. As a global, Match can be used in any number of procedures, but it is always reset by the next call to Matchup().
Carefully type in Listing One, TOOLBOX.ACT, and store a copy to disk before you compile and run it.
The sample Caller() procedure shows you how to use Wordfind() and Matchup(). In this example, Comlist, the command list, is "DOG CAT COW MULE". When run, the program asks you to type one of the four Words in the command list. Then the program finds the Word in the command list and prints the word and its position in the string.
Kevin Sherratt is a full-time science fiction writer and part-time programmer from London, Ontario. He is currently working on an 800XL text adventure game. This is his first appearance in Antic.