0

此代码接收一个 web url (nytimes.com),并输出前 10 个单词出现的列表以及它们出现的次数。我得到了前 10 个单词,但计数为零。有人可以帮我修复计数变量以显示出现次数吗?谢谢!

 local http = require("socket.http")
 local url = "http://www.nytimes.com"
 local body = http.request(url)
 local words = {}

 for word in string.gmatch(body,"%a+") do
   -- print(word)
   words[word] = (words[word] or 0) + 1
 end

 for word, count in pairs(words) do
  -- print(words,count)
 end

function top1(t)
   local max = 0
   local maxword 
   for word, count in pairs(t) do
     if count > max then
       max = count
       maxword = word
     end
   end 
   t[maxword] = nil
   return maxword, count
 end

 for i = 1, 10 do
   print(top1(words))
 end
4

1 回答 1

1

您从 top1() 返回了错误的变量 -return maxword, count应该是return maxword, max.

于 2012-07-26T06:38:11.113 回答