* Merging two lua tables @ 2022-08-29 12:20 Aditya Mahajan via ntg-context 2022-08-29 12:29 ` Aditya Mahajan via ntg-context 2022-08-29 14:28 ` Hans Hagen via ntg-context 0 siblings, 2 replies; 8+ messages in thread From: Aditya Mahajan via ntg-context @ 2022-08-29 12:20 UTC (permalink / raw) To: mailing list for ConTeXt users; +Cc: Aditya Mahajan 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. Thanks, 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Merging two lua tables 2022-08-29 12:20 Merging two lua tables Aditya Mahajan via ntg-context @ 2022-08-29 12:29 ` Aditya Mahajan via ntg-context 2022-08-29 17:33 ` BPJ via ntg-context 2022-08-29 14:28 ` Hans Hagen via ntg-context 1 sibling, 1 reply; 8+ messages in thread From: Aditya Mahajan via ntg-context @ 2022-08-29 12:29 UTC (permalink / raw) To: Aditya Mahajan via ntg-context; +Cc: Aditya Mahajan 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Merging two lua tables 2022-08-29 12:29 ` Aditya Mahajan via ntg-context @ 2022-08-29 17:33 ` BPJ via ntg-context 2022-08-29 18:10 ` Hans Hagen via ntg-context 0 siblings, 1 reply; 8+ messages in thread From: BPJ via ntg-context @ 2022-08-29 17:33 UTC (permalink / raw) To: mailing list for ConTeXt users; +Cc: BPJ [-- Attachment #1.1: Type: text/plain, Size: 1961 bytes --] 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 > > ___________________________________________________________________________________ > [-- Attachment #1.2: Type: text/html, Size: 3114 bytes --] [-- Attachment #2: table-merge.lua --] [-- Type: application/octet-stream, Size: 679 bytes --] local function merge_tables (...) tab = table.pack(...) rv = {} for i=1,tab.n do -- Is it a table? Ignore if not! if 'table' == type(tab[i]) then for k,v in pairs(tab[i]) do -- Is the key a number? if 'number' == type(k) then -- Append it! rv[#rv+1] = v else -- Non-numeric key: overwrite value rv[k] = v end end end end return rv end dump = require"pl.pretty".dump tab_a = { 'a', 'b', 'c', foo = 'A', bar = 'B', baz = 'C', } tab_b = { 'd', 'e', 'f', baz = 'D', bop = 'E', fop = 'F', } merged = merge_tables(tab_a, 'quux', tab_b, 0.0) dump(merged) [-- Attachment #3: Type: text/plain, Size: 496 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Merging two lua tables 2022-08-29 17:33 ` BPJ via ntg-context @ 2022-08-29 18:10 ` Hans Hagen via ntg-context 2022-08-30 8:56 ` BPJ via ntg-context 0 siblings, 1 reply; 8+ messages in thread From: Hans Hagen via ntg-context @ 2022-08-29 18:10 UTC (permalink / raw) To: ntg-context; +Cc: Hans Hagen On 8/29/2022 7:33 PM, BPJ via ntg-context wrote: > 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. you really want something like this then: local tab = table.pack(...) local rv = {} > 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: I might add table.himerged (hash and index) but slightly different (and some 30% faster) to util-tab but i think merging mixed hash/indexed tables is rare. Hans ----------------------------------------------------------------- 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 / 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Merging two lua tables 2022-08-29 18:10 ` Hans Hagen via ntg-context @ 2022-08-30 8:56 ` BPJ via ntg-context 2022-08-30 10:08 ` BPJ via ntg-context 0 siblings, 1 reply; 8+ messages in thread From: BPJ via ntg-context @ 2022-08-30 8:56 UTC (permalink / raw) To: mailing list for ConTeXt users; +Cc: BPJ [-- Attachment #1.1: Type: text/plain, Size: 1890 bytes --] Den mån 29 aug. 2022 20:13Hans Hagen via ntg-context <ntg-context@ntg.nl> skrev: > On 8/29/2022 7:33 PM, BPJ via ntg-context wrote: > > 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. > > you really want something like this then: > > local tab = table.pack(...) > local rv = {} > Thanks. I was manually translating from MoonScript (https://moonscript.org) where variables are local by default. > > 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: > I might add table.himerged (hash and index) but slightly different (and > some 30% faster) to util-tab but i think merging mixed hash/indexed > tables is rare. > > Hans > > ----------------------------------------------------------------- > 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 / > 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 > > ___________________________________________________________________________________ > [-- Attachment #1.2: Type: text/html, Size: 3270 bytes --] [-- Attachment #2: Type: text/plain, Size: 496 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Merging two lua tables 2022-08-30 8:56 ` BPJ via ntg-context @ 2022-08-30 10:08 ` BPJ via ntg-context 0 siblings, 0 replies; 8+ messages in thread From: BPJ via ntg-context @ 2022-08-30 10:08 UTC (permalink / raw) To: bpj; +Cc: BPJ, mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 2194 bytes --] Come to think of it you may also want to check that numeric indices are indeed positive integers before appending their value. I usually don't because their not being so is rare. Den tis 30 aug. 2022 10:56BPJ <bpj@melroch.se> skrev: > > > Den mån 29 aug. 2022 20:13Hans Hagen via ntg-context <ntg-context@ntg.nl> > skrev: > >> On 8/29/2022 7:33 PM, BPJ via ntg-context wrote: >> > 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. >> >> you really want something like this then: >> >> local tab = table.pack(...) >> local rv = {} >> > > Thanks. I was manually translating from MoonScript (https://moonscript.org) > where variables are local by default. > > >> > 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: >> I might add table.himerged (hash and index) but slightly different (and >> some 30% faster) to util-tab but i think merging mixed hash/indexed >> tables is rare. >> >> Hans >> >> ----------------------------------------------------------------- >> 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 / >> 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 >> >> ___________________________________________________________________________________ >> > [-- Attachment #1.2: Type: text/html, Size: 3919 bytes --] [-- Attachment #2: Type: text/plain, Size: 496 bytes --] ___________________________________________________________________________________ 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Merging two lua tables 2022-08-29 12:20 Merging two lua tables Aditya Mahajan via ntg-context 2022-08-29 12:29 ` Aditya Mahajan via ntg-context @ 2022-08-29 14:28 ` Hans Hagen via ntg-context 2022-08-30 5:52 ` Aditya Mahajan via ntg-context 1 sibling, 1 reply; 8+ messages in thread From: Hans Hagen via ntg-context @ 2022-08-29 14:28 UTC (permalink / raw) To: ntg-context; +Cc: Hans Hagen On 8/29/2022 2:20 PM, 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. table.imerge(m1,t1, t2) ----------------------------------------------------------------- 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 / 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Merging two lua tables 2022-08-29 14:28 ` Hans Hagen via ntg-context @ 2022-08-30 5:52 ` Aditya Mahajan via ntg-context 0 siblings, 0 replies; 8+ messages in thread From: Aditya Mahajan via ntg-context @ 2022-08-30 5:52 UTC (permalink / raw) To: Hans Hagen via ntg-context; +Cc: Aditya Mahajan On Mon, 29 Aug 2022, Hans Hagen via ntg-context wrote: > On 8/29/2022 2:20 PM, 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. > table.imerge(m1,t1, t2) Ah, thanks! 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 ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2022-08-30 10:08 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-08-29 12:20 Merging two lua tables Aditya Mahajan via ntg-context 2022-08-29 12:29 ` Aditya Mahajan via ntg-context 2022-08-29 17:33 ` BPJ via ntg-context 2022-08-29 18:10 ` Hans Hagen via ntg-context 2022-08-30 8:56 ` BPJ via ntg-context 2022-08-30 10:08 ` BPJ via ntg-context 2022-08-29 14:28 ` Hans Hagen via ntg-context 2022-08-30 5:52 ` Aditya Mahajan via ntg-context
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).