求Lua 实现单词计数的代码
答案:1 悬赏:40 手机版
解决时间 2021-04-03 02:19
- 提问者网友:你给我的爱
- 2021-04-02 19:07
求Lua 实现单词计数的代码
最佳答案
- 五星知识达人网友:逃夭
- 2021-04-02 19:36
给你个全功能的wc (linux命令wc 即word count)
local BUFSIZE = 2^13 -- 8K
local f = io.input(arg[1]) -- open input file
local cc, lc, wc = 0, 0, 0 -- char, line, and word counts
for lines, rest in io.lines(arg[1], BUFSIZE, "*L") do
if rest then lines = lines .. rest end
cc = cc + #lines
-- count words in the chunk
local _, t = string.gsub(lines, "%S+", "")
wc = wc + t
-- count newlines in the chunk
_,t = string.gsub(lines, "\n", "\n")
lc = lc + t
end
print(lc, wc, cc) --打印行数,单词数,字符数
示例来自 《Programming in Lua third edition》
local BUFSIZE = 2^13 -- 8K
local f = io.input(arg[1]) -- open input file
local cc, lc, wc = 0, 0, 0 -- char, line, and word counts
for lines, rest in io.lines(arg[1], BUFSIZE, "*L") do
if rest then lines = lines .. rest end
cc = cc + #lines
-- count words in the chunk
local _, t = string.gsub(lines, "%S+", "")
wc = wc + t
-- count newlines in the chunk
_,t = string.gsub(lines, "\n", "\n")
lc = lc + t
end
print(lc, wc, cc) --打印行数,单词数,字符数
示例来自 《Programming in Lua third edition》
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯