#!/usr/bin/env lualocal counter={}for line in io.lines()dofor word in string.gmatch(line,"%w%w%w%w+")do
counter[word]=(counter[word]or0)+1endendlocal words={}for word inpairs(counter)do
words[#words+1]=word
end
table.sort(words,function(w1,w2)return counter[w1]>counter[w2]or counter[w1]==counter[w2]and w1<w2
end)local n=math.min(tonumber(arg[1])or math.huge,#words)for i=1,n do
io.write(words[i],"\t",counter[words[i]],"\n")end
11.2
#!/usr/bin/env lua--第一个参数是要输出的单词个数,第二个参数是包含要忽略的单词的文本文件local ignore={}if arg[2]thenfor line in io.lines(arg[2])dofor word in string.gmatch(line,"%w+")do
ignore[word]=trueendendendlocal counter={}for line in io.lines()dofor word in string.gmatch(line,"%w%w%w%w+")doifnot arg[2]or ignore[word]~=truethen
counter[word]=(counter[word]or0)+1endendendlocal words={}for word inpairs(counter)do
words[#words+1]=word
end
table.sort(words,function(w1,w2)return counter[w1]>counter[w2]or counter[w1]==counter[w2]and w1<w2
end)local n=math.min(tonumber(arg[1])or math.huge,#words)for i=1,n do
io.write(words[i],"\t",counter[words[i]],"\n")end
文章目录11.111.2仓库11.1#!/usr/bin/env lualocal counter={}for line in io.lines() do for word in string.gmatch(line,"%w%w%w%w+") do counter[word]=(counter[word] or 0)+1 endendlocal words={}for word in pairs(counter) do words[#words+1]=wordendtable.