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