/* wordCount - count up number of lines, words, and chars in a * file. Similar to unix wc utility. */ to usage() /* Print usage summary and exit. */ { punt(" wordUse - count how many time each word is used in files. usage: wordUse file(s) "); } if (args.size < 1) usage(); class wordUse // Counts number of time word is used. { string word; int count; } dir of wordUse wordDir = (); for (fileName in args) { file f = fileOpen(fileName, "r"); for (;;) { string line = f.readLine(); if (!line) break; int pos, string word; for (;;) { (word, pos) = line.nextToken(pos); if (!word) break; wordUse use = wordDir[word]; if (!use) { use = (word, 0); wordDir[word] = use; } use.count += 1; } } } for (use in wordDir) print(use); /* Now try it again without the structure, just using a dir of int */ print("---- And again using library more heavily ----"); dir of int counts = (); for (fileName in args) { string s = fileReadAll(fileName); array of string words = s.tokens(); for (word in words) counts[word] += 1; } array of string keys = counts.keys(); keys.sort(); for (key in keys) { print("$key: " + counts[key]); }