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:l...@pontex.cz
Pontex s. r. o.      | mailto:pon...@pontex.cz | http://www.pontex.cz
Bezová 1658
147 14 Praha 4

Tel: +420 241 096 751
Fax: +420 244 461 038

Attachment: Count2.cld
Description: Binary data

Attachment: Count2.pdf
Description: Adobe PDF document

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to