O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Flash Hacks
By Sham Bhangal
June 2004
More Info

HACK
#44
Store a List of All Input Words
Accumulate a list of popular words for the purposes of searching, indexing, or text autocomplete and store it in a local file
The Code
[Discuss (0) | Link to this hack]

The Code

The following code achieves this (assuming the code in is also present and that the Submit button's onRelease( ) event handler invokes the function entered( )).

function entered( ) {
  var newWords:Array = new Array( );
  newWords = myText_txt.text.split(" ");
  for (var i = 0; i < newWords.length; i++) {
    if (myText.indexOf(newWords[i].toString( )) == -1) {
      // New word not in dictionary,
      // so add it and re-sort the dictionary.
      myText += " " + newWords[i];
      dictionary.push(newWords[i]);
      dictionary.sort( );
  }
}

Lines 2 and 3 of this function create a new array, newWords, consisting of all the words in the text field myText_text. If you had multiple text fields in your form, you could simply concatenate them all into one string with something like this instead of the current line 3:

newWords = (myTextField1_txt.text + " " +
            myTextField2_txt.text).split(" ");

The for loop's body (lines 4-11) searches the dictionary for every word in the text field (you could, of course, limit it to words with three or more letters). If the new word is not found, it is added to the dictionary, and the dictionary is re-sorted.

Although this code uses the text in the string myText for a fast search , it uses the dictionary array to store new words . You can see this in action if you test the movie in Debug Movie mode (Control→Debug Movie) by looking at the array _root.dictionary as you enter "aardvark." When you click the Submit button, you will see that dictionary[0] now stores the word "aardvark." If you backspace until you have deleted "aardvark" and then start to retype it, Flash autocompletes the word once you enter "aa".


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.