I like the Luatex code of Luigi. Though it is longer than what Wolfgang suggested (and in this case, I am using Wolfgang's solution now that I know about it), the luatex code seems to be the kind of programming that I am more comfortable with. In fact, in Latex when I was trying to achieve this, I used the python.sty package to code this in Python. The optimal solution is that one of Wolfgang that uses \expanded . The others are less than optimal.
To be able to do this using Lua (Luatex), do I need to learn the Lua programming language. Do I pick up any beginners book on Lua? Any recommendations based on experience of those who use Luatex here. Programming in Lua , see www.lua.org
For example, one of the lines in the code is
tprint = function(s) tex.sprint(tex.ctxcatcodes,s) end
Is this Lua or LuaTex?
luatex -- luigi