Hashtable
From Processing
(Redirected from Hash tables)
| Versions: | 1.0+ |
| Contributors: | companje |
| Started: | 2006-06-21 |
If you need Hash tables in Processing have a look at the code below. This snippet shows how to count the number of occurances of each word in a textfile.
Source Code
/** hashtable taken from http://www.companje.nl @author Rick Companje */ String lines[]; String list[]; void setup() { lines = loadStrings(“Fok.txt”); println(“there are “ + lines.length + ” lines”); Words words = new Words(); for (int i=0; i < lines.length; i++) { list = split(lines[i]); for (int j=0; j < list.length; j++) { words.addWord(list[j]); } } words.printWords(); } class Words { Hashtable ht=new Hashtable(); void addWord(String txt) { Word word; if (ht.containsKey(txt)) { word = (Word)ht.get(txt); word.count++; } else { word = new Word(txt); } ht.put(txt,word); } void printWords() { for (Enumeration e = ht.keys() ; e.hasMoreElements() ;) { String name = e.nextElement().toString(); Word word = (Word)ht.get(name); println(word.txt + “: “ + word.count); } } } class Word { public String txt; public int count = 1; Word(String txt) { this.txt = txt; } }