BRIGHAM YOUNG UNIVERSITY
CHum LiveCode Gateway
Office of Digital Humanities
Array Exercise—Dictionary array
- Create a text file (using BBEdit, Notepad or any other text editor). This text file will contain a list of keys and element contents. We'll use a simple dictionary as an example. The lines in the text file should be formatted like this:
keyword (tab) definition (return)
Here's a sample text file:
bread edible thing made out of grain
milk nutritious liquid produced by female mammals
rock a hard thing
table a flat surface raised from the floor on legs
carpet a floor made of fibers
printer a device for producing printed text on a page
car a mechanical device for conveying people and goods
- Create a stack, and place two fields on the card, named "keyword" and "definition".
- Create a button called "make" or something similar. In this button write a handler that will read your text file into a variable. Then use the
split command to make the text into an array. The keys in our text file are separated from the element content by a tab, and the elements are separated from one another by returns. So your split command might look like this:
split gDictionary by return and tab
Be sure to make your array variable a global variable so that all other scripts in your stack can have access to the array.
- Create a button called "addToArray". Write a mouseUp handler for the button that puts the definition field into an array, using the keyword field as the key. For example:
put fld "definition" into gDictionary[fld "keyword"]
- Remember that you will want to declare the dictionary variable as global, because you'll want to have access to it in other scripts.
- Now enter a few keyword and definition pairs.
- Once you have several pairs saved, create a way to look up the definitions. Make a button called "lookup". In the mouseUp handler, ask the user to type in a word to look up. (Don't forget to declare the gDictionary variable as global here, too!) You might use:
ask "What word do you want to look up?"
- During a lookup operation, the user should enter a keyword and the definition should be returned. That might look something like this:
- Think about ways to make your lookup program a better user experience. For example, what will happen if the user cancels the ask dialog? One of the hallmarks of well-written software is its ability to robustly handle all user inputs.
- Figure out a way to display a list of the available keywords to look up? By clicking on a word in the list you could display the definition the word. The
keys function will help you here.
- Once this clickable list was working, how could you make sure it always has the most up-to-date list of keywords in it? Modify your stack to do this.
Once your stack is working the way you want, turn it in to the Homework Drop folder on the server.
CHum LiveCode Gateway
Maintained by Devin Asay
Copyright © 2005 Brigham Young University