s = io.read("*all") local words = {} for word in string.gmatch(s, "%S+") do words[word] = (words[word] or 0) + 1 end s = nil sorted = {} for k,v in pairs (words) do table.insert(sorted, { word = k, count = v }) end table.sort(sorted, function (a,b) return(a.count > b.count) end) for i,v in ipairs(sorted) do io.write (v.word, " ", v.count, "\n") end