From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from zero.zsh.org (zero.zsh.org [IPv6:2a02:898:31:0:48:4558:7a:7368]) by inbox.vuxu.org (Postfix) with ESMTP id 2B3602A5E1 for ; Mon, 15 Jan 2024 01:56:23 +0100 (CET) ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1705280183; b=jMgkd/HfOMPseOWLzz25CfLi+YBbqKquAozJwkx9ZKxYu1WtbHkLhDMcRCBy7aOY2bUbuzJVfS chHJgXC6+zvyC/09UIw79ZfFj8m7994aXpzA8qwgvOYDhBnz1jSx91luL4jol4IEiV+anBy5jA 95NfkunKvfriTa11xUEqxCMRHO5lVVhd3xVfgzCoTEP32Hlrl18SX13mj0U+FvGSh1PRStBa0m M3hiIuZlAnGErma6YtFk0WBmS/QDhsgK7z5L0KpaYVt8EF4hXXKLkyQv3DLJHDTTEXGJWGILfz jQLjD7ymzZT6yfgaB5Su+R+pda2VEOrUIzlTmbI/NXwE4g==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f52.google.com) smtp.remote-ip=209.85.218.52; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1705280183; bh=Izw0bulRakHG5l7PImjRqLjEgSB6opbBKbyKX2U3E5c=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:Cc:To:Subject: Message-ID:Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature: DKIM-Signature; b=V+RpnTmCNrJOkr2Mw6FMlpjadQFGrMsGX9D7KD1Mqhc0xFe6/RBP3HAMYRrUsTKFo2fYkZQVzi i89DdaqNaAsFzPhB8G5eb+X2tt9WDijPmi9acfXtN9W/eIq2togTNQ4A2dh08iknAKZFe8sg/e 5sWcVC9X5+PfIWy2uvr8zLlwgve6qmHmzVKzDOMKrcNZ1T2h3dRngL35UQ3TF2p83jhVbaa8fh 1G4aYgf0wQ6tEM910DTD7zTli5h+HDETVPBk1jVg3RUS8ve5pv/14IsdLH94CeZuGcprQkDcUz uLc6B3z6SyVhxmSnI4YrmcrcgayTKR26JQ2ttG9zwKx5bw==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=Db0got+jYs7EIpBVKXvAqPg/myQGYVODY1fTD3oeKoU=; b=IDB5fBLFQi7XpwnfUXC6Z3pfqn zZ06UO1iaCZZ0ckZKfNlJTSlYi1ZXDsYlT0s+4JmuvgqJGxH44f1rb5ARPjcFCSAbKEzPFI3wvfNM 3MgccTD/nCz8usBSf3JSVpkxyLqgpTcCsTmkB8GO86lFqwhJBeJhMgY9CYLFdIz9frv4UTSEh7sjl YMHcS5Cwhg1mAnUf/v1NW34jxRgiLZf5hL+Lh6kaM7SK7ObChJbWlUMlwyvVNQTU82jorp8LvHZKj J3+/btOcIrAZeG21uPH/sm6kZtIzsHPX+QNAUVx73KvXJRzkrJIysV+EgRLFiSC7YgEZNXjr/mntt TNb/qqdg==; Received: by zero.zsh.org with local id 1rPBH5-00045a-FC; Mon, 15 Jan 2024 00:56:23 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f52.google.com) smtp.remote-ip=209.85.218.52; dkim=pass header.d=brasslantern-com.20230601.gappssmtp.com header.s=20230601 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ej1-f52.google.com ([209.85.218.52]:48402) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1rPBGV-0003O5-LZ; Mon, 15 Jan 2024 00:55:49 +0000 Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-a2cad931c50so259067666b.1 for ; Sun, 14 Jan 2024 16:55:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20230601.gappssmtp.com; s=20230601; t=1705280147; x=1705884947; darn=zsh.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Db0got+jYs7EIpBVKXvAqPg/myQGYVODY1fTD3oeKoU=; b=GxqdZD4oU6wkMr0KKdMJsb62we4Gy5Ozc16+T9LUlGqkgvJKJQ5JnKxYI2FdrzCX+L T2IYLBJyYa54GykWjicgDaXg89B18hS6488itJQdlEqSqK2snSYKbmL5iTpqlv2JjqY6 bkEhQ/di2PWcI74FcffJconP5C4aO/b6+lY2+QnKqswnimCEje4LFCaWKgitFdrK8NUl TTG03yPH8VKVgHSLMp4ZHxjwopjQjwWATAWqEM9DhfCRi+O0nff2sEjbUbJpBTqfBPe0 CqECdBeFV36l4jbxLhraBjeAMikUD5NUEDNhTpKWVsa9pTKl3aGBLGNnS6GOWzzPJoWF LFqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705280147; x=1705884947; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Db0got+jYs7EIpBVKXvAqPg/myQGYVODY1fTD3oeKoU=; b=ian1KEBoHLUKvf7gup2bWXx+o8XnG//JsZ5m/Aha/PXKiGQOJeOQEYR4VpnhuPXo7O 7cf3raaWTeR8yEMgwQsYlYqeG84ChJEHiL4srIlGW78LqhM/yS0z1+t4/PCDmnjgG4Bs P0YTI379hxfn1iegSKzkICI1132h+z4g/vzU2V+AtaKpJHfVNAORU1jt+beT8LY+YrH9 5a0MbpAqjcJHrhEy1o6uamShDImmXdpsLL+n5gLElrYBi73/0+mBfzCvE47k3BK7TMJa ZUqv3CHijsP3GbUX7hudGlNva9pSDr9Mk7Opqs11GDObe8OBSeqEXGYwjB7cfnSDrVv8 cakA== X-Gm-Message-State: AOJu0YxS79gLTrqLkIMvUvEk1avGIOISVIy3pb9iKAnko+n39a81NOAZ mahPymWt+zb5pp+b30InWJBu0UpiTz40IwA9zQpcqVaXAae0jM9XnL6DrHsdtw== X-Google-Smtp-Source: AGHT+IEXNPO3/DzzoN2i6l8HX//HnGFJX33V1vM2ChSmbTx4OrBxu71MZTe7Uzrs5orbM0jQMyAs+ai3EHqYnvsbEt4= X-Received: by 2002:a17:907:7f25:b0:a2b:58cc:7d6b with SMTP id qf37-20020a1709077f2500b00a2b58cc7d6bmr2609042ejc.23.1705280146285; Sun, 14 Jan 2024 16:55:46 -0800 (PST) MIME-Version: 1.0 References: <205735b2-11e1-4b5e-baa2-7418753f591f@eastlink.ca> <7b20b425-59b2-466d-a9aa-a2768d1d4633@app.fastmail.com> <4b475319-ef78-4d46-a62c-27c3b0af3ea4@eastlink.ca> In-Reply-To: <4b475319-ef78-4d46-a62c-27c3b0af3ea4@eastlink.ca> From: Bart Schaefer Date: Sun, 14 Jan 2024 16:55:35 -0800 Message-ID: Subject: Empty element elision and associative arrays (was Re: Slurping a file) To: Ray Andrews Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 29480 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: On Sun, Jan 14, 2024 at 4:03=E2=80=AFPM Ray Andrews wrote: > >> Elision of empty values is a property of unquoted expansion. It >> has NOTHING to do with variable types. > > I'll take another look, thanks. I may have gotten my wires crossed. When assigning to an associative array ... % typeset -A asc=3D( $something ) ... the expansion of $something has to yield an even number of "words", yes. But remember the previous lesson about those outer parens -- they only mean that the thing to the left is an array, they don't matter to what's inside those parens. So if $something is an array with empty elements, those elements are going to be elided, which might leave you with an odd number of words and break the assignment -- or possibly worse, turn some values into keys and some keys into values. Another thing that may be confusing you is that associative arrays only expand their values by default, and an empty value will be elided just like any other empty array element. % typeset -A asc=3D( one 1 two 2 three 3 nil '' ) % typeset -p asc typeset -A asc=3D( [nil]=3D'' [one]=3D1 [three]=3D3 [two]=3D2 ) % printf "<<%s>>\n" ${asc} <<1>> <<2>> <<3>> % printf "<<%s>>\n" "${asc[@]}" <<1>> <<2>> <<3>> <<>> (output may vary because associative arrays are not ordered). Also, an empty key can have a value: % asc+=3D( '' empty ) % typeset -p asc typeset -A asc=3D( ['']=3Dempty [nil]=3D'' [one]=3D1 [three]=3D3 [two]=3D2 = ) % printf "<<%s>>\n" ${asc} <> <<1>> <<2>> <<3>> But if you reference the keys, the empty one will be elided when not quoted= : % printf "<<%s>>\n" ${(k)asc} <> <> <> <> % printf "<<%s>>\n" "${(@k)asc}" <<>> <> <> <> <> Returning to the original example, that means if you're copying one associative array to another, you need to copy both the keys and the values, and quote it: % typeset -A asc=3D( "${(@kv)otherasc}" )