From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/115859 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: BPJ via ntg-context Newsgroups: gmane.comp.tex.context Subject: Re: Merging two lua tables Date: Mon, 29 Aug 2022 19:33:38 +0200 Message-ID: References: Reply-To: bpj@melroch.se, mailing list for ConTeXt users Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="000000000000a0ba6205e764aaa0" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27932"; mail-complaints-to="usenet@ciao.gmane.io" Cc: BPJ To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Mon Aug 29 19:34:27 2022 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane-mx.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oSieY-00075b-Qr for gctc-ntg-context-518@m.gmane-mx.org; Mon, 29 Aug 2022 19:34:26 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 475E42A4126; Mon, 29 Aug 2022 19:33:55 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZR-5RAA3sb2r; Mon, 29 Aug 2022 19:33:53 +0200 (CEST) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id DC64A2A4127; Mon, 29 Aug 2022 19:33:52 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id B71162A4125 for ; Mon, 29 Aug 2022 19:33:50 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kq-xGxcvMnY3 for ; Mon, 29 Aug 2022 19:33:49 +0200 (CEST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.208.49; helo=mail-ed1-f49.google.com; envelope-from=melroch@gmail.com; receiver= Original-Received: from mail-ed1-f49.google.com (mail-ed1-f49.google.com [209.85.208.49]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 96F392A4117 for ; Mon, 29 Aug 2022 19:33:49 +0200 (CEST) Original-Received: by mail-ed1-f49.google.com with SMTP id 2so11070783edx.2 for ; Mon, 29 Aug 2022 10:33:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=CmKv1lKT6Vc64n9NJ1FdGGOnHYzMKw6WPZxcsulBPRo=; b=zW/8C8N8WhP7Dz4cmt2dzNCy38wcwadImMoh4sppqK6dDEPAIHNsJdC6b7Z8pdlz51 SmIVkaRlHIvAQ/w7NWKjh5gJgBLt9Wlc68ZkiSdho1DzShjh9tfc+YffX0Fpa05eiTdf MvG+ccBXbjqmZ4q+Vi3vnwZ2YVDqxPojmGzw/FaiF5gLjRAAQiL+4jKGnyn9VWGg6dlL qKLxap2U8TWdqYL0XjDsYENGd5+7qiIs0MJ7PrAF6KnQ0+2nMhJvCHZLn602RvhpTmXT fp3iwV8aKkdfu97t0J2GH3yd8lMWwVIguGEpiG5PcI7V6pDx32v2NUcbWbuBL9WpelCH CCbQ== X-Gm-Message-State: ACgBeo2h2YUC4wtUq2boczvYaqTpIk67I05TRLnbdkz0N8CSHQRLjdXL hXeKjIuJdSa+yuGY3QO2aXjue36X4QaFhTRGk17k1rid X-Google-Smtp-Source: AA6agR6EaU4KvSsDUl9gOOtRMupnlsIWoksxZwoiv+pQj7bC49BY9Gb4oZ67a8btv6jXQbNyfbdn9Hj9goTgJfT4OKU= X-Received: by 2002:a05:6402:190d:b0:447:ed22:84eb with SMTP id e13-20020a056402190d00b00447ed2284ebmr13096721edz.379.1661794428944; Mon, 29 Aug 2022 10:33:48 -0700 (PDT) In-Reply-To: X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.26 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.io gmane.comp.tex.context:115859 Archived-At: --000000000000a0ba6205e764aaa0 Content-Type: multipart/alternative; boundary="000000000000a0ba6005e764aa9e" --000000000000a0ba6005e764aa9e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D {...}` and ipairs, or implement pack: ``````lua local function pack (...) return { n =3D select('#', ...), ... } end `````` Den m=C3=A5n 29 aug. 2022 14:32Aditya Mahajan via ntg-context 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 =3D { 1, 2 } > > local t2 =3D { 8, 9 } > > > > local m1 =3D {} > > table.merge(m1,t1, t2) > > table.print(m1) > > > > local m2 =3D 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 silent= ly > 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 > > _________________________________________________________________________= __________ > --000000000000a0ba6005e764aa9e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

I use th= e attached Lua function to merge array, map and mixed tables alike (but dif= ferently! :-) The trick is to check if each key is numeric or not, append i= f 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 use= s table.pack. You might need to use `tab =3D {...}` and ipairs, or implemen= t pack:

``````lua
<= div dir=3D"auto">local function pack (...)
=C2=A0 re= turn { n =3D select('#', ...), ... }
end
``````


Den m=C3=A5n 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.mer= ged should do the trick, but I cannot figure out how to use them.
>
> ```
> local t1 =3D { 1, 2 }
> local t2 =3D { 8, 9 }
>
> local m1 =3D {}
> table.merge(m1,t1, t2)
> table.print(m1)
>
> local m2 =3D table.merged(t1, t2)
> table.print(m2)
> ```
>
> Processing the file with context filename shows that both m1 and m2 ar= e {8, 9}. What am I missing.

Looking at the code, I see what is happening. table.merge(d) assume that th= e tables are key-value tables so the keys of the first table are silently o= verwritten 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 t= he Wiki!

maillist : ntg-context@ntg.nl / https:= //www.ntg.nl/mailman/listinfo/ntg-context
webpage=C2=A0 : https://www.pragma-ade.nl / http:= //context.aanhet.net
archive=C2=A0 : https://bitbucket.org/ph= g/context-mirror/commits/
wiki=C2=A0 =C2=A0 =C2=A0: https://contextgarden.net
___________________________________________________________________________= ________
--000000000000a0ba6005e764aa9e-- --000000000000a0ba6205e764aaa0 Content-Type: application/octet-stream; name="table-merge.lua" Content-Disposition: attachment; filename="table-merge.lua" Content-Transfer-Encoding: base64 Content-ID: <182eaa9f622e99c8a031> X-Attachment-Id: 182eaa9f622e99c8a031 bG9jYWwgZnVuY3Rpb24gbWVyZ2VfdGFibGVzICguLi4pCiAgdGFiID0gdGFibGUucGFjayguLi4p CiAgcnYgID0ge30KICBmb3IgaT0xLHRhYi5uIGRvCiAgICAtLSBJcyBpdCBhIHRhYmxlPyBJZ25v cmUgaWYgbm90IQogICAgaWYgJ3RhYmxlJyA9PSB0eXBlKHRhYltpXSkgdGhlbgogICAgICBmb3Ig ayx2IGluIHBhaXJzKHRhYltpXSkgZG8KICAgICAgICAtLSBJcyB0aGUga2V5IGEgbnVtYmVyPwog ICAgICAgIGlmICdudW1iZXInID09IHR5cGUoaykgdGhlbgogICAgICAgICAgLS0gQXBwZW5kIGl0 IQogICAgICAgICAgcnZbI3J2KzFdID0gdgogICAgICAgIGVsc2UKICAgICAgICAgIC0tIE5vbi1u dW1lcmljIGtleTogb3ZlcndyaXRlIHZhbHVlCiAgICAgICAgICBydltrXSA9IHYKICAgICAgICBl bmQKICAgICAgZW5kCiAgICBlbmQKICBlbmQKICByZXR1cm4gcnYKZW5kCgpkdW1wID0gcmVxdWly ZSJwbC5wcmV0dHkiLmR1bXAKCnRhYl9hID0gewogICdhJywgJ2InLCAnYycsCiAgZm9vID0gJ0En LAogIGJhciA9ICdCJywKICBiYXogPSAnQycsCn0KCnRhYl9iID0gewogICdkJywgJ2UnLCAnZics CiAgYmF6ID0gJ0QnLAogIGJvcCA9ICdFJywKICBmb3AgPSAnRicsCn0KCm1lcmdlZCA9IG1lcmdl X3RhYmxlcyh0YWJfYSwgJ3F1dXgnLCB0YWJfYiwgMC4wKQoKZHVtcChtZXJnZWQpCiAgCg== --000000000000a0ba6205e764aaa0 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cHM6Ly93d3cubnRnLm5sL21haWxtYW4v bGlzdGluZm8vbnRnLWNvbnRleHQKd2VicGFnZSAgOiBodHRwczovL3d3dy5wcmFnbWEtYWRlLm5s IC8gaHR0cDovL2NvbnRleHQuYWFuaGV0Lm5ldAphcmNoaXZlICA6IGh0dHBzOi8vYml0YnVja2V0 Lm9yZy9waGcvY29udGV4dC1taXJyb3IvY29tbWl0cy8Kd2lraSAgICAgOiBodHRwczovL2NvbnRl eHRnYXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCg== --000000000000a0ba6205e764aaa0--