BYU Home pageBRIGHAM YOUNG UNIVERSITY
  Office of Digital Humanities
Back     BYU LiveCode Lessons Gateway

Array Exercise—Dictionary array

This exercise is intended to give you basic experience with creating, adding to, and saving array data in LiveCode. When you are finished you should know:

  1. How to create an array.
  2. How to use the split command to convert a text file into an array.
  3. How to reference elements of an array variable using array [ ] notation syntax.
  4. How to get a list of keys of an array using the keys function.
  5. How to use the arrayEncode() and arrayDecode() functions to save array data to an external file.

An important secondary objective of this assignment is to get you thinking about designing your stack to produce a good end-user experience.

Instructions

  1. 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 covering made of fibers
    printer	a device for producing printed text on a page
    car	a mechanical device for conveying people and goods
    
  2. Create a stack, and place two fields on the card, named "keyword" and "definition".

  3. Create a button called "makeArray" 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.

  4. 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:
  5. put fld "definition" into gDictionary[fld "keyword"]
  6. Remember that you will want to declare the variable gDictionary as global, because you'll want to have access to it in other scripts.

  7. Now enter a few keyword and definition pairs.

  8. 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:
  9. ask "What word do you want to look up?"
  10. During a lookup operation, the user should enter a keyword and the definition should be returned. That might look something like this:
  11. answer gDictionary[it]
  12. 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.

  13. 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.

  14. 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.

Assignment

In class we did all 11 points above. To complete this assignment do the following to your stack:

Once your stack is working the way you want, turn it in to the Assignment_Drop folder on the server. Don't forget to turn in any external storage files together with your stack. (Making them into a .zip archive is a good way of doing this.)

I have put together an example of the kind of "polished", end-user-ready stack that I would like you to build. It is on the DigHT 310 server in Lesson_materials/arrays/ArrayEx-Key.zip. Notice how much of my scripting was dedicated to improving the user experience. (E.g., hiding and showing objects so they appear only when needed, checking for the presence of needed data before allowing the addition of terms, etc.)


Back     BYU LiveCode Lessons Gateway
Maintained by Devin Asay.
Copyright © 2005 Brigham Young University