From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: easy calling of associative array?
Date: Tue, 3 Nov 2015 07:57:30 -0800 [thread overview]
Message-ID: <151103075730.ZM18820@torch.brasslantern.com> (raw)
In-Reply-To: <5637EB66.9050301@eastlink.ca>
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.
next prev parent reply other threads:[~2015-11-03 15:57 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-01 23:12 Ray Andrews
2015-11-01 23:55 ` ZyX
2015-11-02 0:49 ` Ray Andrews
2015-11-02 3:08 ` Bart Schaefer
2015-11-02 3:33 ` Ray Andrews
2015-11-02 6:51 ` Bart Schaefer
2015-11-02 15:43 ` Ray Andrews
2015-11-02 16:28 ` Bart Schaefer
2015-11-02 18:32 ` Ray Andrews
2015-11-02 19:37 ` ZyX
2015-11-02 22:10 ` Bart Schaefer
2015-11-02 22:50 ` Ray Andrews
2015-11-02 21:05 ` Bart Schaefer
2015-11-02 23:01 ` Ray Andrews
2015-11-03 15:57 ` Bart Schaefer [this message]
2015-11-04 14:48 ` Ray Andrews
2015-11-04 16:39 ` Bart Schaefer
2015-11-04 18:31 ` Ray Andrews
2015-11-02 19:33 ` ZyX
2015-11-02 23:04 ` Ray Andrews
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=151103075730.ZM18820@torch.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).