class wordCount // Class to keep track of number of times a word is used. { string word; // Our word int count; // How many times we've seen it } flow cmpWordCount(wordCount a,b) into (int diff) // Compare two wordCounts in such a way as to help // sort them with most commonly used word first. { diff = b.count - a.count; } // Turn file into an array of words. string text = fileReadAll(args[0]); array of string words = text.tokens(); dir of wordCount counts = (); // Loop through words creating a new wordCount // object if we haven't seen word yet, otherwise // incrementing count on existing wordCount object. for (word in words) { word = word.lower(); wordCount wc = counts[word]; if (wc) wc.count += 1; else { wc = (word, 1); counts[word] = wc; } } // Build up an array of wordCounts and then sort it. array of wordCount countedWords = (); for (wc in counts) countedWords.append(wc); countedWords.sort(cmpWordCount); // Finally do the output for (wc in countedWords) print(wc.word + " " + wc.count);