Hi,

I use the attached Lua function to merge array, map and mixed tables alike (but differently! :-) The trick is to check if each key is numeric or not, append if it is and overwrite if it isn't.

This function just ignores non-table arguments because that is what I usually want. You may want to throw an error instead. Also it uses table.pack. You might need to use `tab = {...}` and ipairs, or implement pack:

``````lua
local function pack (...)
  return { n = select('#', ...), ... }
end
``````


Den mån 29 aug. 2022 14:32Aditya Mahajan via ntg-context <ntg-context@ntg.nl> skrev:
On Mon, 29 Aug 2022, Aditya Mahajan via ntg-context wrote:

> Hi,
>
> How do I merge two lua tables? I believe that table.merge or table.merged should do the trick, but I cannot figure out how to use them.
>
> ```
> local t1 = { 1, 2 }
> local t2 = { 8, 9 }
>
> local m1 = {}
> table.merge(m1,t1, t2)
> table.print(m1)
>
> local m2 = table.merged(t1, t2)
> table.print(m2)
> ```
>
> Processing the file with context filename shows that both m1 and m2 are {8, 9}. What am I missing.

Looking at the code, I see what is happening. table.merge(d) assume that the tables are key-value tables so the keys of the first table are silently overwritten by the send. I guess, I'll have to write my own function to merge "array" tables.

Aditya
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________