[NTG-context] final thoughts on experiments with lua

luigi scarso luigi.scarso at gmail.com
Thu Jan 10 15:53:21 CET 2019

On Thu, Jan 10, 2019 at 12:11 PM Henning Hraban Ramm <texml at fiee.net> wrote:

>  a Lua pairs table, the order can be arbitrary.

sure ,  the *default*  __pairs gives pseudo-arbitrary order, but you can
always use metatable:
-- test.lua
local _c,_t=0,{}
local t = {}
setmetatable(t, {
   __newindex = function(tbl, k, v)
      rawset(tbl, k, v)

  __pairs = function(tbl)
      local i = 1
      return function(tbl,k)
        k = _t[i]
        return k,tbl[k]
      end, tbl,nil

t['a']= 'aaa'
t['3a']= '3aa'
t['b']= 'baa'
t['2b']= '2ba'
t['c']= 'caa'
t['2c']= '2ca'
t['d']= 'daa'
t['2d']= '2da'
t['e']= 'eaa'
t['2e']= '2ea'

for k,v in pairs(t) do print(k,v) end

$ mtxrun --script test.lua
a aaa
3a 3aa
b baa
2b 2ba
c caa
2c 2ca
d daa
2d 2da
e eaa
2e 2ea

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20190110/9c4e95a4/attachment.html>

More information about the ntg-context mailing list