lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Francisco, thank you very much for pointing this out. Sets me on track.


As suggested by some, I add a little bit of information to illustrate why and in what context my question arose.

I am programming for the ConTeXt (a TeX variant) typesetting programming environment. There is exchange of values from the TeX main program body to the embedded Lua in this system and vice versa. It brings its own peculiarities. More often than not parameters are exchanged from TeX to Lua as strings. So some limitations one must take for granted. Or otherwise clutter the interface with different macro's for string or bare value exchange. I have learnt to live with those limitations.

Programming of data structures in the TeX language (although formally a Turing machine) can be done, but is a pain in the ass, so to speak. Therefore I am doing that on ConteXt's Lua side.
In order to handle data structures like arrays and records in TeX I am aiming for a few object oriented modules for each of these two. It then seemed natural to traverse each of them in their own way.


On 22 Oct 2023, at 13:27, Francisco Olarte <folarte@peoplecall.com> wrote:

May be having apairs() to will make more sense, but anyway, due to how
they work ( returning values plus a function ) building a Xpairs
functions is easy,  just do it:

function inext(t,i) i=i+1 ; v=t[i]; if v then return i,v end end
function zpairs(t) return inext, t, -1 end -- like ipairs, but start at 0.

You can trivially iterate through the string only:

function snext(t,k)
local v; k,v=next(t,k)
while k~= nil do
  if type(k)=="string" then return k,v end
  k,v = next(t,k)
end
end
function spairs(t) return snext, t, nil end
a={1,2,b=3,[{}]=4,c=5,6,7}
for i,v in spairs(a) do print(i,v) end
b    3
c    5

Francisco Olarte.


yours sincerely
dr. Hans van der Meer