ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* lua tables - how do you cope?
@ 2016-07-30 10:20 Schmitz Thomas A.
  2016-07-30 13:04 ` Joseph Canedo
  0 siblings, 1 reply; 9+ messages in thread
From: Schmitz Thomas A. @ 2016-07-30 10:20 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 10:20 lua tables - how do you cope? Schmitz Thomas A.
@ 2016-07-30 13:04 ` Joseph Canedo
  2016-07-30 14:31   ` Wolfgang Schuster
  2016-07-30 22:28   ` Hans Hagen
  0 siblings, 2 replies; 9+ messages in thread
From: Joseph Canedo @ 2016-07-30 13:04 UTC (permalink / raw)
  To: Schmitz Thomas A., mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 319 bytes --]

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.

[-- Attachment #1.2: Type: text/html, Size: 4249 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 13:04 ` Joseph Canedo
@ 2016-07-30 14:31   ` Wolfgang Schuster
  2016-07-30 21:01     ` Schmitz Thomas A.
  2016-07-30 22:28   ` Hans Hagen
  1 sibling, 1 reply; 9+ messages in thread
From: Wolfgang Schuster @ 2016-07-30 14:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Schmitz Thomas A.


[-- Attachment #1.1: Type: text/plain, Size: 654 bytes --]

> 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

[-- Attachment #1.2: Type: text/html, Size: 2707 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 14:31   ` Wolfgang Schuster
@ 2016-07-30 21:01     ` Schmitz Thomas A.
  2016-07-30 21:19       ` Arthur Reutenauer
                         ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Schmitz Thomas A. @ 2016-07-30 21:01 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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 <schuster.wolfgang@gmail.com> 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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 21:01     ` Schmitz Thomas A.
@ 2016-07-30 21:19       ` Arthur Reutenauer
  2016-07-30 21:46       ` Lukas Prochazka
  2016-07-30 22:26       ` Hans Hagen
  2 siblings, 0 replies; 9+ messages in thread
From: Arthur Reutenauer @ 2016-07-30 21:19 UTC (permalink / raw)
  To: Mailing list for ConTeXt users

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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 21:01     ` Schmitz Thomas A.
  2016-07-30 21:19       ` Arthur Reutenauer
@ 2016-07-30 21:46       ` Lukas Prochazka
  2016-07-30 22:25         ` Schmitz Thomas A.
  2016-07-30 22:26       ` Hans Hagen
  2 siblings, 1 reply; 9+ messages in thread
From: Lukas Prochazka @ 2016-07-30 21:46 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 21:46       ` Lukas Prochazka
@ 2016-07-30 22:25         ` Schmitz Thomas A.
  0 siblings, 0 replies; 9+ messages in thread
From: Schmitz Thomas A. @ 2016-07-30 22:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> On 30 Jul 2016, at 23:46, Lukas Prochazka <lpr@pontex.cz> 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
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 21:01     ` Schmitz Thomas A.
  2016-07-30 21:19       ` Arthur Reutenauer
  2016-07-30 21:46       ` Lukas Prochazka
@ 2016-07-30 22:26       ` Hans Hagen
  2 siblings, 0 replies; 9+ messages in thread
From: Hans Hagen @ 2016-07-30 22:26 UTC (permalink / raw)
  To: ntg-context

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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: lua tables - how do you cope?
  2016-07-30 13:04 ` Joseph Canedo
  2016-07-30 14:31   ` Wolfgang Schuster
@ 2016-07-30 22:28   ` Hans Hagen
  1 sibling, 0 replies; 9+ messages in thread
From: Hans Hagen @ 2016-07-30 22:28 UTC (permalink / raw)
  To: ntg-context

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
-----------------------------------------------------------------
___________________________________________________________________________________
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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-07-30 22:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-30 10:20 lua tables - how do you cope? Schmitz Thomas A.
2016-07-30 13:04 ` Joseph Canedo
2016-07-30 14:31   ` Wolfgang Schuster
2016-07-30 21:01     ` Schmitz Thomas A.
2016-07-30 21:19       ` Arthur Reutenauer
2016-07-30 21:46       ` Lukas Prochazka
2016-07-30 22:25         ` Schmitz Thomas A.
2016-07-30 22:26       ` Hans Hagen
2016-07-30 22:28   ` Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).