2012-07-26 58 views
0

此代码需要一个网址(nytimes.com),并输出前10个单词出现次数及其出现次数的列表。我收到了前10个单词,但我没有计数。有人可以帮助我修复count变量以显示出现次数吗?谢谢!在网页中计数字符串的出现次数?

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 

回答

1

你返回从TOP1错误的变量() - return maxword, countreturn maxword, max

相关问题