[NTG-context] final thoughts on experiments with lua

Hans Hagen j.hagen at xs4all.nl
Thu Jan 10 13:09:33 CET 2019


On 1/10/2019 12:11 PM, Henning Hraban Ramm wrote:
> Am 2019-01-10 um 10:50 schrieb luigi scarso <luigi.scarso at gmail.com>:
> 
>>> sections = { “1”, “2”, “2a” }
>>>
>>> words = { [“1”] = { “a”, “b” },
>>>            [“2a”] = { “c”, “d” } }
>>>
>>> so I can iterate through ipairs(sections) in sequence and pick up the word lists for each section. In the greater scheme of things, as Hraban pointed out: if there were an “ordered table” structure in Lua, this is precisely what it would do behind the scenes; it would just make it easier for the user.
>>>
>> the point is that I believe that is also doable in lua...
>> maybe could be helpful to have a significative example in python, ton see if we can mimic it in lua  ?
> 
> The "minimal example" in Python is a collections.OrderedDict. It’s not about ordering the entries (anew), but keeping the order, i.e. retrieving the entries in the same order as you added them. If you iterate over a Python dict or a Lua pairs table, the order can be arbitrary.

I'll add this:

local t = table.orderedhash()

t["1"]  = { "a", "b" }
t["2"]  = { }
t["2a"] = { "a", "c", "d" }

for k, v in table.ordered(t) do
     print(k)
     inspect(v)
end

which gives

1
table={
  "a",
  "b",
}
2
table={
}
3
table={
  "a",
  "c",
  "d",
}



-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the ntg-context mailing list