[NTG-context] Evaluating a Lua expression at the end
Procházka Lukáš Ing. - Pontex s. r. o.
LPr at pontex.cz
Mon Apr 13 16:48:23 CEST 2015
Hello,
another single-pass approach, which uses the fact that you know the questions and their point "weights" in advance is to form questions into a Lua table and evaluate the total first:
----
local C = context
local tab =
{ {"Question 1", 10, },
{"Question 2", 20, },
{"Question 3", 30, },
}
local n = 0
for _, v in ipairs(tab) do local q, n1 = unpack(v); n = n + n1 end
-- Or: for _, v in ipairs(tab) do n = n + v[2] end
local f = function(it)
local q, n1 = unpack(it)
n = n + n1
C(q .. ": " .. n)
C.par()
end
C.starttext()
C("Total score: " .. n)
C.par()
n = 0 -- Reset
f(tab[1])
f(tab[2])
f(tab[3])
-- Or: for _, v in ipairs(tab) do f(v) end
C.stoptext()
----
Best regards,
Lukas
> Thanks for the response. I am looking at something like this:
>
> ==============
> \starttext
> \directlua{total = 0}
> \title{Homework 1}
>
> Total score: \directlua{tex.print(total);}
>
> Question 1 (3 points)
> \directlua{total = total + 3}
>
> Question 2 (4 points)
> \directlua{total = total + 4}
>
> Question 3 (5 points)
> \directlua{total = total + 5}
>
> \stoptext
> ==============
>
> I expect the “Total score” to be followed by “12”, but I don't know
> how to get that (or whether that is even possible).
>
> Thanks.
>
> Kumar
--
Ing. Lukáš Procházka | mailto:LPr at pontex.cz
Pontex s. r. o. | mailto:pontex at pontex.cz | http://www.pontex.cz
Bezová 1658
147 14 Praha 4
Tel: +420 241 096 751
Fax: +420 244 461 038
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Count2.cld
Type: application/octet-stream
Size: 436 bytes
Desc: not available
URL: <http://www.ntg.nl/pipermail/ntg-context/attachments/20150413/96fa6a3e/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Count2.pdf
Type: application/pdf
Size: 6563 bytes
Desc: not available
URL: <http://www.ntg.nl/pipermail/ntg-context/attachments/20150413/96fa6a3e/attachment.pdf>
More information about the ntg-context
mailing list