[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)
      _c=_c+1
      _t[_c]=k
      rawset(tbl, k, v)
   end,

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


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

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