From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22601 invoked by alias); 3 Nov 2015 15:57:39 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20892 Received: (qmail 23581 invoked from network); 3 Nov 2015 15:57:38 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern_com.20150623.gappssmtp.com; s=20150623; h=from:message-id:date:in-reply-to:comments:references:to:subject :mime-version:content-type; bh=8Rdp6pqgFdqiOfOjf+lC4wtP0eArRwLfIoFrNecSJ2g=; b=I9KBj2bKY9CHmQyIOVFL8ZaOqC+JCBz1QRz+tBtjtB2lHMukNkN+kNTrGi1WPZJk/Y /4Rz7s14fKJvNuMZVkaH5R48RjG/PYTDXCjZzRGcgzXlc5/6YXKAsLhzIK95OLM7jC+Z MJU/zyszWFD8N0NinthJ6p3bm3liLLup84pZYUg7Fa2pj9+hgnwyCE5v6Nb4jECeW+9W OvHzyLpeq3BGz3C+riqciNRqaM1m6SMeC/yL59KARKS7GZR+jt5USAMIcrJkl0DSvp1P vE6T4VH54Yj1x3ZCmEOLAIdFuvDgokb/iQyvgstUxpoq5eGiXER3sQmbcadaVau5rpMV x6Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:message-id:date:in-reply-to:comments :references:to:subject:mime-version:content-type; bh=8Rdp6pqgFdqiOfOjf+lC4wtP0eArRwLfIoFrNecSJ2g=; b=CgVzCXCoTX7UQ44bCpguUK/KVt1Wk+XJE35szVR0XBm0Ouq35nOOACtpwAKdqbPaBY 3N2fov8B4p8ljE1QB9yDrln3NzLFM73Bp6wOa1iTT/zD8EOQevLkdaUxXJKPYkIaMcAY kTd+6LYU8j1njT8q3MjlT4Q7XKLuUxNhi5CgqYhLheyecDpmvG2UQfMvmW0I8nq1ZtOj OiZoWfHcLeFkY76dwzI64xBpw1qA6J35/mvOLMufAFbJq7d0ZAev3hXeKIXDP6HOfFSZ mYc9qU/qEjhekena3G5JA5YPP1bOZ6xnckzHhxd+F/q96t1TisUd7FTP6hS3EBSY57fG G7SA== X-Gm-Message-State: ALoCoQncJHGCv03hNxsy5f6bq77BwDcjGeoc8F//q3z23mxM2i8Cx/n5g9WEDP681E2bhOnEuyZt X-Received: by 10.202.199.142 with SMTP id x136mr18280494oif.121.1446566253189; Tue, 03 Nov 2015 07:57:33 -0800 (PST) From: Bart Schaefer Message-Id: <151103075730.ZM18820@torch.brasslantern.com> Date: Tue, 3 Nov 2015 07:57:30 -0800 In-Reply-To: <5637EB66.9050301@eastlink.ca> Comments: In reply to Ray Andrews "Re: easy calling of associative array?" (Nov 2, 3:01pm) References: <56369C7B.2030604@eastlink.ca> <1237641446422150@web6m.yandex.ru> <5636B333.8060300@eastlink.ca> <151101190842.ZM16752@torch.brasslantern.com> <5636D99F.2030807@eastlink.ca> <151101225100.ZM16882@torch.brasslantern.com> <563784B5.3040901@eastlink.ca> <151102082808.ZM17640@torch.brasslantern.com> <5637AC27.8010007@eastlink.ca> <151102130518.ZM17830@torch.brasslantern.com> <5637EB66.9050301@eastlink.ca> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: easy calling of associative array? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii On Nov 2, 3:01pm, Ray Andrews wrote: } Subject: Re: easy calling of associative array? } } On 11/02/2015 01:05 PM, Bart Schaefer wrote: } > } > typeset -g ary[idx]=val } > } > Ordinarly "typeset" inside a function body behaves like "local". The } > -g option tells it not to do that, so that the name "ary" is taken to } > come from the calling context instead of the current function context. } } I don't understand. The idea of context here is new to me. Why is } 'ary' not a variable like any other? Parameters have dynamic scope from the function call (not the function name) where they are declared. So if you have a function "upper" that calls a function "middle" that calls a function "lower", a parameter that is declared in "middle" is visible to "lower" but not to "upper", and a parameter declared in "upper" is visible to both "middle" and "lower". "typeset" is the generic "declare a parameter" builtin. All of the other builtins (delcare, local, integer, etc.) are special cases of "typeset". So if both "upper" and "middle" contain "typeset -A ary", the name "ary" in "middle" is now a new variable, not the same as the "ary" in "upper". This is exactly like saying "local ary" in "middle", becuse "local" is really a form of "typeset", merely given another name to make it more obvious what's happening. Using "typeset -gA ary" in "middle" prevents this; it says "don't declare a new local parameter, use the one that is already in scope." The usage with "-g ary[idx]" says "don't attempt to index into a new local parameter $ary, instead index into the existing $ary if there is one." Which is what you want if the name "ary" has been passed down through $1.