zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Nikolai Weibull <zsh-users-list@pcppopper.org>
Cc: zsh-users@sunsite.dk
Subject: Re: Associative Arrays
Date: Thu, 21 Oct 2004 09:40:14 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.61.0410210900110.14956@toltec.zanshin.com> (raw)
In-Reply-To: <20041021123835.GF9224@puritan.pcp.ath.cx>

On Thu, 21 Oct 2004, Nikolai Weibull wrote:

> * Stephane Chazelas <Stephane.Chazelas@morse.com> [Oct 21, 2004 13:10]:
> > > I'm have a few questions regarding associative arrays.  What exactly 
> > > is the A flag for parameter expansion for?  I mean, how does one use 
> > > it, and how does one use the AA flag?
> >
> > : ${(AA)=a::=foo bar}
> 
> aha, OK.  How very useful.

Obviously one would be more likely to use it as ${(AA)=a:=foo bar} (note
only one colon) where it assigns only if the hash is empty.

> > > Is there any way to pass an associative array as ONE argument to a 
> > > function and easily deal with it as the same associative array in 
> > > the other function?

I note in passing that this is not just an issue with associative arrays. 
The shell language (not just zsh's language) in general lacks a way to 
truly pass by reference rather than by value.

> > > The easiest way seems to be the "pass-by-reference" technique, where 
> > > one simply passes the name of the array and then use the P flag to 
> > > deal with it.

That's one way.  Another way is simply to make use of dynamic scoping, so
that the called function treats the variable as global, and the calling
function restricts the scope when necessary.  Part of the function's API
then becomes the name of the variable that it expects to manipulate.

(This is one reason why ksh has "namerefs" so that you can explicitly 
alias two names to the same value, which is after all what happens 
implicitly in other languages when a call-by-reference parameter is 
passed.)

Or am I misreading the question?  Are you asking how to pass-by-value an
associative array?

> > You could use "${${(@qqkv)A}[*]}"
> >
> > So that in the function you can have
> >
> > typeset -A local_hash
> > local_hash=("${(Q@)${(z)1}}")
> 
> Aha, double quotation and then dequoting.  What a bitch...is this
> seriously the simplest/only ways to do it?

If you want to pass its entire value as a single positional parameter, 
that's pretty close to the only way.  However, you have the entire set
of positional parameters to play with.

Think like a Perl programmer, and imagine you're trying to pass a set of 
name-value pairs as @_.  You'd probably write something like

 sub blather {
   my $unnamed1 = shift;
   my $unnamed2 = shift;
   my %named = @_;
   if ($named{'winnie'} eq 'pooh') { print "Oh, stuff and bother\n"; }
 }

So in zsh that'd be

 function blather {
   local unnamed1=$1; shift
   local unnamed2=$2; shift
   local -A named; set -A named "$@"
   if [[ $named[winnie] = pooh ]]; then print "Oh, stuff and bother\n"; fi
 }

> I'd say that shell programming would be a lot simpler as a whole if this 
> was simpler.

Hey, it's only been a few years now that shells have had associative 
arrays AT ALL.  Imagine what shell programming was like in the old days.


  reply	other threads:[~2004-10-21 16:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-20 19:53 Nikolai Weibull
2004-10-21 11:05 ` Stephane Chazelas
2004-10-21 12:38   ` Nikolai Weibull
2004-10-21 16:40     ` Bart Schaefer [this message]
2004-10-21 20:22       ` Nikolai Weibull
2004-10-22 12:32       ` Stephane Chazelas
2004-10-22 15:07         ` Bart Schaefer
2004-10-25 13:14           ` Stephane Chazelas
2004-10-21 23:22     ` Eric Mangold
2004-10-22  0:08       ` Nikolai Weibull

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=Pine.LNX.4.61.0410210900110.14956@toltec.zanshin.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users-list@pcppopper.org \
    --cc=zsh-users@sunsite.dk \
    /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).