From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 2306 invoked from network); 11 Dec 2022 19:54:21 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 11 Dec 2022 19:54:21 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1670788461; b=gSyYmzI4sNLRoxRdWE2CFvNwKlgVRc1zAmwxr3VCaDDvnsEkPgi/60FBXy+SfiX+NdlgPsFAgc WUIK2lU14RE3czo5IIs1Gut90YL4USrfLQG0oI2DL/FqARBd7I8JEsiFkQx/N7o+C9phnt+SHg +u6UCb8EIuzEzbHGjU0bYPtlQqeL63HxsvzpOyr4l26DblCZdsVQqT7bPZ1e8bFLUPl8t0nHr2 UTpWHou98Vd0CLLEUHB/2TczfcyFbOWY82nCf7IudfgJXUNMGqAfqmdcbxfJsvKt/QXxIyu5Pi LfAW6bamshAZltnOkUkReMm80bmhLC69sZv6QCZ/76iH/A==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ej1-f43.google.com) smtp.remote-ip=209.85.218.43; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 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=1670788461; bh=GjQBw/XGlTLklcOVQIwhY0zf81daCp6Zt0OW4mgW7Jg=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=eBBCa3201c38bQGL70zeshvv8zcNUeWHr4/+H6rqNBkGAU+pw9Lbt/AM7L8V/3aF+yU5fsVTcD SUG/7LVn74qw6eiREBoqBVszPQtMmCiy9R0vypDVTgSIQhca5gdsDGfqykf3MAnlSoPAUf4ncW 3nxFzVX797Vh/L8BZk7+kV8I3IrTxQKQfNbbcNKKEstkx6kZ5kgofeZ7gyYC0JbAiqP/vrX9Y4 uN5kZtiJF0uuvvHSDZk+3PTYUq8vtq5OHevjjoDQNGYHrOoV9Rd0jlySr9TGqcPzjSnI+uaBaz w9YYIXGGAJTfLSc6tpwEetz3zN51KAwfCQLWxzN4NDClgw==; 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-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=hilOHos2TLvIEuPegYs39zLOu6JPCZR2zBQWPCnaqsI=; b=arG+ZW0JGtH1fpbVxbu7eiSKEY arEcdDSuc35XT8GPPWM+BZk/nLtmrqUyJHf9yNuckLu7L+YZLEwzd0IaEJlkckT/lxMh6vZYopkX/ Fsvk6CF/QeW6Lzkk7s8gQWTlrpRr+XsQLKeD3bUKzv6mLf4Tpl93mAkbEzHPY7aoK3XvajBKm2viN JJmS1DedNnHWenSFhC1/Jv0srNlErn3VjrCB8lLQgSOKJozad2jiSYeLoV4I97sKNFLX2RIDpxMZM Iv1CNojHGb6bFZjcagl8Ant6GTRa7K6KGfdsOAxhgNurhP7dv3m4aXu4jctHqPDcvj3NHiGL5cmxy CYRAkcmA==; Received: by zero.zsh.org with local id 1p4SOy-0005y4-QN; Sun, 11 Dec 2022 19:54:20 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ej1-f43.google.com) smtp.remote-ip=209.85.218.43; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ej1-f43.google.com ([209.85.218.43]:43633) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1p4SO8-0005FY-62; Sun, 11 Dec 2022 19:53:28 +0000 Received: by mail-ej1-f43.google.com with SMTP id kw15so23117857ejc.10 for ; Sun, 11 Dec 2022 11:53:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=hilOHos2TLvIEuPegYs39zLOu6JPCZR2zBQWPCnaqsI=; b=Hm8+omtOiKhB2j4dG9S6b8haoZwh5XhdcKcWTYRAuLlRgrqRg0PcMf/Lx342ADukAW 5k0nh9tzDa1Illjy7/Nlv13PgmvrcPjSo0q1gXy8MPZohq04SgQx1tDjetJz6xLtNIBg QviPRifO71uBRod+cSg9IKgkfUeWTw20s+FQH6Ea/X1Po15sk2Q7VudpR1X4C95+qY6O +xy6FgR4f3zhwEAhDSeK1TzkIGEUF0MflzbSB8w7SHCEQ4iStbMDQhTZ7dZKej/+6s+K z+LfVngyuLLUAcnZeWa1Rx2NQFUjPoTSZfJszmYlmvasCi2xMjE+slHJMq701Bc0QKTb gSAw== 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:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hilOHos2TLvIEuPegYs39zLOu6JPCZR2zBQWPCnaqsI=; b=JvlFweN5B0wTjWbLKSbstryxDnOY+Larr3kmBEmggqhc1TS+yDtv5uh2nZBw6ds9rN 25MAr1O1M5fq1rD0GTPGqHXHQiZ94Vt88iMxJExHe7YbXuaU++GcYJiFyTi1g5zF0t1j Z24cUN4VC3MJrMHok/zACKqhxq5C64P8cC+BeU+7e820wiC0+fl7wj7v/SrNi3M9ZaH7 nYuKjgFLKvAtsAh23iVz81+k7bWWfI0CIrUgmGeqKsRc53KCmxr53h0DRyWHltdYe7jx kjRCaJHn5heWuaOjaOXHqshywUNl2LpHYbnzESqeYWCWPhI8j+tgIc8H705JBY1Tn1t7 HIug== X-Gm-Message-State: ANoB5plMpTgD2/w6s66/Ycmwv9LUfB6GGOFfJNskQLpF5gbxgKgFHXCx GfYrmajC113wxOTr6YteW47H7qeHA9LcRL1sWtao84HgdivCnA== X-Google-Smtp-Source: AA0mqf4VmB8s4u9AVkFADzX9Ln7Cusks3F2ZOwrXWTH5LGfPfiHA83ltp8Mkf3C7iB0tEcmgcsgJtFd95943kl3q1kk= X-Received: by 2002:a17:906:3b0a:b0:7c1:449b:dabb with SMTP id g10-20020a1709063b0a00b007c1449bdabbmr446906ejf.732.1670788407376; Sun, 11 Dec 2022 11:53:27 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Bart Schaefer Date: Sun, 11 Dec 2022 11:53:16 -0800 Message-ID: Subject: Re: associative array questions To: Ray Andrews Cc: Zsh Users Content-Type: text/plain; charset="UTF-8" X-Seq: 28517 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, Dec 11, 2022 at 9:40 AM Ray Andrews wrote: > >[...] C-brained as I am, I'm wanting to link to > the input array directly via a pointer sort of operation so in test3 I'm > trying to grab the name of the target array and modify it (the array) > directly. There are no pointers to data structures in zsh. Parameter reference is always by value. Even ${(P)other} returns a value, it's just the value of the parameter whose name is in turn the value of $other. Anything you do with the result of ${(P)other} acts on that value, not on the indirectly-named parameter itself. There is one sort-of exception: The ${NAME=WORD} and related assign-and-substitute operations. E.g., if you use ${(P)other::=text}, then the parameter named by $other is assigned the value "text". I say this is only sort-of an exception because if $other names an array, you can only assign to the entire array, not to individual subscripted fields. If you want to assign indirectly to a particular field (whether a position in an ordinary array or a named element in an associative) you have to store the entire subscript expression in $other, such as other="aa[second]" : ${(P)other::='tea for two'} > print "aa[second] is: ${${(P)aarray}[second]}" Incidentally, this doesn't work in zsh earlier than 5.2. Even in 5.9, it doesn't carry over into the ${NAME=WORD} construct. > # BAD: > # ${(P)aarray}[second]='tea for two' Assignments are only parsed as such when there is an identifier (string of nothing but alphanumerics or underscore, plus optional subscript) to the left of the equal sign BEFORE any substitutions are performed. So this doesn't "look like" an assignment to the parser. Lacking the outer ${...} enclosing the subscript, ${(P)aarray} expands to all the values of the pointed-to associative array, and you get the equivalent of "1" "two" "three blind mice"[second] > # GOOD: > eval "${aarray}[second]='tea for two'" In this case the "eval" turns the command into aa[second]='tea for two' which satisfies the "identifier to the left" rule. You could also do (another thing that doesn't work in older zsh) eval "${aarray}+=([second]='tea for two')" > '${(P)aarray'} should expand to 'aa' remembering that it's a variable Nope. It expands to a value, not a variable reference. Zsh doesn't (yet) have what ksh calls "namerefs" ... block out everything you know about pointers in C. > also unexpected that the array prints upside down: Associative arrays are unordered hash tables. It might print "upside down" or "inside out" depending on what keys are present. They're only even called "arrays" because that's how the value behaves if you omit the subscript.