This is less a specific question about ConTeXt than a hope for good advice: I’m maltreating my xml files with a mixture of TeX and Lua. I want to extract and typeset information in different forms, so I first collect everything in lua tables, rearrange and order these tables and typeset the results. All fine and dandy. My problem is that I have to have tables within tables within tables… you get the picture. One aspect of Lua that is really bugging me is the fact that associative tables have no order, which can be a pain in the butt for this kind of operation. So I have to be careful that I have to use constructs that will keep the order in which items have been added and loop through them via ipairs() instead of pairs(). I find it difficult to keep track of what’s inside my layers upon layers of tables. So my question is: how do those of you who are more experienced with this kind of question proceed? Do you have any handy tool to visualize a table? Any tips you want to share? Thanks a lot! Thomas
If the requirement is to iterate on a table having the keys, values sorted by key (assuming the keys can be sorted), there are ways to do this. Please see http://lua-users.org/wiki/SortedIteration for an example (this just replaces pairs(t) with orderedPairs(t)). Hope this helps Joseph De : Schmitz Thomas A.
Joseph Canedo mailto:josephcanedo@gmail.com 30. Juli 2016 um 15:04
If the requirement is to iterate on a table having the keys, values sorted by key (assuming the keys can be sorted), there are ways to do this. Please see http://lua-users.org/wiki/SortedIteration for an example (this just replaces pairs(t) with orderedPairs(t)).
\starttext \startluacode local testtable = { z = "A", y = "B", x = "C" } for i, j in next, testtable do context("%s:%s",i,j) context.par() end context.blank() for i, j in table.sortedhash(testtable) do context("%s:%s",i,j) context.par() end \stopluacode \stoptext Wolfgang
Thank you, but this is not what I’m looking for. I know how to sort a table, and I know the Lua table tutorial (the Lua wiki is, IMHO, really terrible and disorganized). I have to construct deeply nested tables and sometimes lose track of what is at what level of my table, so I was wondering if there was an easy way of visualizing a nested table. On the web, you can find a number of (mostly abandoned) projects; the one at http://siffiejoe.github.io/lua-microscope/ says: "Many Lua programmers have written their own pretty-printer or data dumper and some even use it for (de-)serializing Lua data structures.” So I was wondering if any of the Lua users here on the list has something they want to share. Thomas
On 30 Jul 2016, at 16:31, Wolfgang Schuster
wrote: If the requirement is to iterate on a table having the keys, values sorted by key (assuming the keys can be sorted), there are ways to do this. Please see http://lua-users.org/wiki/SortedIteration for an example (this just replaces pairs(t) with orderedPairs(t)).
\starttext
\startluacode
local testtable = { z = "A", y = "B", x = "C" }
for i, j in next, testtable do context("%s:%s",i,j) context.par() end
context.blank()
for i, j in table.sortedhash(testtable) do context("%s:%s",i,j) context.par() end
\stopluacode
\stoptext
On Sat, Jul 30, 2016 at 11:01:29PM +0200, Schmitz Thomas A. wrote:
Thank you, but this is not what I’m looking for. I know how to sort a table, and I know the Lua table tutorial (the Lua wiki is, IMHO, really terrible and disorganized). I have to construct deeply nested tables and sometimes lose track of what is at what level of my table, so I was wondering if there was an easy way of visualizing a nested table. On the web, you can find a number of (mostly abandoned) projects; the one at http://siffiejoe.github.io/lua-microscope/ says: "Many Lua programmers have written their own pretty-printer or data dumper and some even use it for (de-)serializing Lua data structures.” So I was wondering if any of the Lua users here on the list has something they want to share.
Well, there’s table.serialize from the ConTeXt core, which fits nicely in the description you quote. Best, Arthur
Hello Thomas, here is my "dump()" I've been using for several years: ---- function dump(arg, opts) -- .seen, .pfx if type(opts) == "string" then print(opts); opts = nil elseif opts == true then print("-- (dump)"); opts = nil end local pfx = opts and opts.pfx local seen = opts and opts.seen or {} if type(arg) == "table" then if pfx then pfx = pfx .. "][" else pfx = "[" --seen = {} end seen[arg] = tostring(arg) --true local keys = {} do -- Sort keys, if all are strings local strs_only = true for k in pairs(arg) do if strs_only and type(k) ~= "string" then strs_only = false end keys[#keys + 1] = k end if strs_only then table.sort(keys) end end for _, key in ipairs(keys) do local val = arg[key] io.write(pfx .. tostring(key) .. "] = " .. tostring(val) .. "\t(" .. type(val) .. ")") if type(val) == "table" then if seen[val] then print(" (seen)") else print() dump(val, {pfx = pfx .. tostring(key), seen = seen}) --pfx .. tostring(key), seen) end else print() end end else print(arg) end end ---- Try: ---- a = {c = 1, b = 2}; a.a = a dump(a) dump(a, "This is 'a'.") ---- Improvements or parametrization of visualizing style would be possible, of course... Best regards, Lukas ----- Original Message ----- From: Schmitz Thomas A. [mailto:thomas.schmitz@uni-bonn.de] To: mailing list for ConTeXt users [mailto:ntg-context@ntg.nl] Sent: Sat, 30 Jul 2016 23:01:29 +0100 Subject: Re: [NTG-context] lua tables - how do you cope?
Thank you, but this is not what I’m looking for. I know how to sort a table, and I know the Lua table tutorial (the Lua wiki is, IMHO, really terrible and disorganized). I have to construct deeply nested tables and sometimes lose track of what is at what level of my table, so I was wondering if there was an easy way of visualizing a nested table. On the web, you can find a number of (mostly abandoned) projects; the one at http://siffiejoe.github.io/lua-microscope/ says: "Many Lua programmers have written their own pretty-printer or data dumper and some even use it for (de-)serializing Lua data structures.” So I was wondering if any of the Lua users here on the list has something they want to share.
Thomas
On 30 Jul 2016, at 23:46, Lukas Prochazka
wrote: Hello Thomas,
here is my "dump()" I've been using for several years:
Arthur, Lukas, these are both great and very helpful, thanks a lot! I feel bad for not knowing table.serialize (which can even be used with the context() function to typeset the result, and I really like Lukas’ step-by-step breakdown of the table. Maybe I’ll try and think of a nice visual way to represent these Lua tables! Thomas
On 7/30/2016 11:01 PM, Schmitz Thomas A. wrote:
Thank you, but this is not what I’m looking for. I know how to sort a table, and I know the Lua table tutorial (the Lua wiki is, IMHO, really terrible and disorganized). I have to construct deeply nested tables and sometimes lose track of what is at what level of my table, so I was wondering if there was an easy way of visualizing a nested table. On the web, you can find a number of (mostly abandoned) projects; the one at http://siffiejoe.github.io/lua-microscope/ says: "Many Lua programmers have written their own pretty-printer or data dumper and some even use it for (de-)serializing Lua data structures.” So I was wondering if any of the Lua users here on the list has something they want to share.
\startluacode context.tocontext(yourtable) \stopluaxcode ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
On 7/30/2016 3:04 PM, Joseph Canedo wrote:
If the requirement is to iterate on a table having the keys, values sorted by key (assuming the keys can be sorted), there are ways to do this. Please see http://lua-users.org/wiki/SortedIteration for an example (this just replaces pairs(t) with orderedPairs(t)).
or just in context for k, v in table.sortedhash(t) print(k,v) end btw, mtxrun --script foo.lua has all that on board as well
Hope this helps
Joseph
*De : *Schmitz Thomas A. mailto:thomas.schmitz@uni-bonn.de *Envoyé le :*samedi 30 juillet 2016 12:21 *À : *mailing list for ConTeXt users mailto:ntg-context@ntg.nl *Objet :*[NTG-context] lua tables - how do you cope?
This is less a specific question about ConTeXt than a hope for good advice: I’m maltreating my xml files with a mixture of TeX and Lua. I want to extract and typeset information in different forms, so I first collect everything in lua tables, rearrange and order these tables and typeset the results. All fine and dandy. My problem is that I have to have tables within tables within tables… you get the picture. One aspect of Lua that is really bugging me is the fact that associative tables have no order, which can be a pain in the butt for this kind of operation. So I have to be careful that I have to use constructs that will keep the order in which items have been added and loop through them via ipairs() instead of pairs(). I find it difficult to keep track of what’s inside my layers upon layers of tables. So my question is: how do those of you who are more experienced with this kind of question proceed? Do you have any handy tool to visualize a table? Any tips you want to share?
Thanks a lot!
Thomas
___________________________________________________________________________________
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
___________________________________________________________________________________
___________________________________________________________________________________ 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 ___________________________________________________________________________________
-- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (6)
-
Arthur Reutenauer
-
Hans Hagen
-
Joseph Canedo
-
Lukas Prochazka
-
Schmitz Thomas A.
-
Wolfgang Schuster