[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [StrongED] Roman numerals
Gavin Wraith, on 15 Aug, wrote:
> In message <mpro.ouqmja002a56b0244.pittdj@xxxxxxxxxxxx>
> David Pitt <pittdj@xxxxxxxxxxxx> wrote:
>
> > sumnums has more than one issue. adding 'require' as in :-
> >
> > in require "lpeg"
> >
> > helped, [well done !] but it now falls over at line 12.
>
> I cannot put my finger on sumnums - must have it somewhere, I
> suppose. If it is meant to add up all the positive integers
> occurring in a text then this ought to do as an unsophisticated
> zero-approximation:
>
> #! lua
> local n = 0
> for line in io.lines (arg[1]) do
> for x in line:gmatch "%d+" do n += tonumber (x) end -- for
> end -- for
> print "Total = ", n
>
That works, but with :-
print ("Total = ", n)
This is the sumnuns I have :-
#! lua
-- Add up numbers within a text
do
io.input(arg[1])
text = io.read("*all")
io.input()
local add = \(x,y) => x+tonumber(y) end
local R,P,C,Ca,Cc in lpeg
local digit, point = R "09", P "."
local num = C(digit^1*point^(-1)*digit^0)/add
local notnum = (1-digit)^1
local pattern = Ca(Cc(0)*(num + notnum)^0)
local result = pattern:match(text)
print(result or 0)
end -- do
This was in a StrongED download.
--
David Pitt
--=20
To unsubscribe send a mail to StrongED+unsubscribe@xxxxxxxxxxx
List archives at http://www.Torrens.org/RO/StrongED/index.html