From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/115864 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: Tue, 30 Aug 2022 12:08:58 +0200 Message-ID: References: Reply-To: bpj@melroch.se, mailing list for ConTeXt users Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1570045512090355578==" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29971"; mail-complaints-to="usenet@ciao.gmane.io" Cc: BPJ , mailing list for ConTeXt users To: bpj Original-X-From: ntg-context-bounces@ntg.nl Tue Aug 30 12:09:52 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 1oSyBs-0007dw-0W for gctc-ntg-context-518@m.gmane-mx.org; Tue, 30 Aug 2022 12:09:52 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id DE6A62A4111; Tue, 30 Aug 2022 12:09:13 +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 5CdYxPdCcMDj; Tue, 30 Aug 2022 12:09:13 +0200 (CEST) Original-Received: from zapf.ntg.nl (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id D15B72A4124; Tue, 30 Aug 2022 12:09:11 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id E34652A40EE for ; Tue, 30 Aug 2022 12:09:10 +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 GzgQxF26mWqS for ; Tue, 30 Aug 2022 12:09:09 +0200 (CEST) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=209.85.218.47; helo=mail-ej1-f47.google.com; envelope-from=melroch@gmail.com; receiver= Original-Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 3CF302A4125 for ; Tue, 30 Aug 2022 12:09:09 +0200 (CEST) Original-Received: by mail-ej1-f47.google.com with SMTP id cu2so21158121ejb.0 for ; Tue, 30 Aug 2022 03:09:09 -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=rqWcv7Ylg2DrhgveHiLqMX2qTISVvz2Di002bE61ol0=; b=NYZZLjGJVmb8Y+ZEAcZpU9LG+AK60XfHcnBH+UqrbnD7rJzxrPmbkxnObqESl8DCRx LuC4snebiHnZoCicPcFlqfwRlJAbLz1vCDmRgioDbAj0kigw/6xGiTwiJwGhD035n5Jm 5eD7VbXidGXCuEkAOqo2DmKuLHDSuSrEbvSY78zzfVfepKrDRTqYkkygZyc5atEg8K2K R1DBb9LHHN1S9iQdcwHxmbQVQYsbAALvVuyUTlc0pfXN1EORp3nCL97uSnRp//RcJFwp 8QCmxZfwbQRtABm6I/A2surc0Q6elx8I8035quwdh5SAoXytCBdcEnsPogdLeXr7wUoO OkQA== X-Gm-Message-State: ACgBeo2TVgJ37XSh9dXWwkbBZ3QHN+kWg/kPwFl1KbeaKNlHMLhhEWKU hVnp7TZ2h/ffN3maTgRIepJ3lSvSm38cBJ+oHpkUkaM3gT8= X-Google-Smtp-Source: AA6agR5u5sCsizeSC9cxvtUdhEoG3JJabUi0C5b0Y+fEtxlkseWrBJIPP5bq9Qb8jXTGcQ+iZ8zHzK48ctoxqgprhtM= X-Received: by 2002:a17:907:6d24:b0:731:7720:bb9b with SMTP id sa36-20020a1709076d2400b007317720bb9bmr16742677ejc.717.1661854148748; Tue, 30 Aug 2022 03:09:08 -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:115864 Archived-At: --===============1570045512090355578== Content-Type: multipart/alternative; boundary="0000000000003468a505e77292d5" --0000000000003468a505e77292d5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 skrev: > > > Den m=C3=A5n 29 aug. 2022 20:13Hans Hagen via ntg-context > 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 =3D table.pack(...) >> local rv =3D {} >> > > Thanks. I was manually translating from MoonScript (https://moonscript.or= g) > 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 =3D {...}` and ipairs, or imple= ment >> > 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 t= o >> 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 >> >> ________________________________________________________________________= ___________ >> > --0000000000003468a505e77292d5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Come to think of it you may also want to check that numer= ic indices are indeed positive integers before appending their value. I usu= ally don't because their not being so is rare.

Den tis 30 aug. 2022 10:5= 6BPJ <bpj@melroch.se> skrev:


Den m=C3=A5n 29 = aug. 2022 20:13Hans Hagen via ntg-context <ntg-context@ntg.nl> sk= rev:
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 a= like
> (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=C2=A0 tab =3D table.pack(...)
local=C2=A0 rv=C2=A0 =3D {}
<= br>
Thanks. I was manually translating from MoonScri= pt (https://moonscript.org) where variables are local by default.


> This function just ignores non-table arguments because that is what I<= br> > 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 imple= ment
> 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

-----------------------------------------------------------------
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Hans Hagen | PRAGMA ADE
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ridderstraat 27 | 80= 61 GH Hasselt | The Netherlands
=C2=A0 =C2=A0 =C2=A0 =C2=A0 tel: 038 477 53 69 | www.p= ragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________= ________
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://bitbu= cket.org/phg/context-mirror/commits/
wiki=C2=A0 =C2=A0 =C2=A0: https://contextgarden.net
___________________________________________________________________________= ________
--0000000000003468a505e77292d5-- --===============1570045512090355578== 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== --===============1570045512090355578==--