$ cat wc
#!/usr/local/bin/lua
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
while true do
local lines, rest = f:read(BUFSIZE, "*line")
if not lines then break end
if rest then lines = lines .. rest .. '\n' end
cc = cc + string.len(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)
This works well. You can test it.
I will herewith explain the lines for you to get a better idea.
This example I lifted from the standard lua manual.
Let us look at first 6 lines:
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
while true do
local lines, rest = f:read(BUFSIZE, "*line")
if not lines then break end
First line stores the buffer size in the variable BUFSIZE.
In lua print(x) is same as =x. So let us check.
> =2^13
8192
>
So it is same as
local BUFSIZE = 8192
which we usually do using C as
#define BUFSIZE 8192 and we use
char buf[BUFSIZE];
"local" is an access specifier. We can omit it.
local f = io.input(arg[1]) -- open input file
This line opens the file given in standard input(argv[1]) as the comment says.
local cc, lc, wc = 0, 0, 0 -- char, line, and word counts
This line initializes the 3 areas of interest for us, char counts,
word counts and line counts.
Unlike C lua can assign multiple variables in the same line.
Next 3 lines,
while true do
local lines, rest = f:read(BUFSIZE, "*line")
if not lines then break end
start the while loop(infinite loop with a break condition within) and
the other lines read the input file line by line storing the lines in
the variable lines and the if condition is the termination condition
for the while loop which is infinite.
We are done with first 6 lines.
Let us look at the rest of the code:
if rest then lines = lines .. rest .. '\n' end
cc = cc + string.len(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)
The first line uses the lua concatenation operator ".." to concatenate
the lines with
the "rest" variable which we used above. I really don't understand this line.
Let us look at the next line. I know this much that it is an if
condition in one line.
cc = cc + string.len(lines)
-- count words in the chunk
These two lines increment the character count. As string.len(foo)
returns the characters in foo string.
Since we initializes cc(character count to zero), this line gets us
the total char count in input file.
local _,t = string.gsub(lines, "%S+", "")
wc = wc + t
These two lines count the words by splitting the line into words using
the string library we saw two days ago.
-- count newlines in the chunk
_,t = string.gsub(lines, "\n", "\n")
lc = lc + t
This chunk counts the newlines by using the same gsub() method. Since
lines variable contains the whole
file, this will give us the total line count in lc.
end
print(lc, wc, cc)
The end is for the while() infinite loop and print() prints our computed values.
-Girish
--
Gayatri Hitech
http://gayatri-hitech.com
--
Gayatri Hitech
http://gayatri-hitech.com
_______________________________________________
ILUGC Mailing List:
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc