zsh-workers
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: Tanaka Akira <akr@jaist.ac.jp>, zsh-workers@sunsite.auc.dk
Subject: Re: assoc array assignment problem.
Date: Tue, 21 Sep 1999 04:59:34 +0000	[thread overview]
Message-ID: <990921045934.ZM13380@candle.brasslantern.com> (raw)
In-Reply-To: <rsq7lllujs2.fsf@crane.jaist.ac.jp>

On Sep 21, 10:37am, Tanaka Akira wrote:
} Subject: assoc array assignment problem.
}
} Z(2):akr@localhost% Src/zsh -f
} localhost% typeset -A arr
} localhost% a='$b'
} localhost% b='c'
} localhost% arr[$a]=d
} localhost% print -lr - ${(kv)arr}
} c
} d
} localhost% 
} 
} Hm. Variable expansion is performed twice.

This is not an associative array problem; it affects all arrays.  Note:

zagzig<17> q=()
zagzig<18> g=7
zagzig<19> f='$g'
zagzig<20> q[$f]=seven
zagzig<21> print $#q $q
7 seven
zagzig<22>

The first substitution happens at the expected time, during evaluation
of the line.  The second one happens at execute time in addvars() --
assignment is implemented as if `x=y somecommand` where somecommand is
null.  addvars() calls setsparam(), which discovers that there is no
parameter named "q[$g]" and therefore creates "q" and then makes a call
to getvalue() to retrieve a Value structure for the parameter that it
just created.  getvalue() then expands $g (as it would for ${q[$g]} in
which the stuff inside the ${ } has _not_ previously been expanded) and
you get the effect above.

And you can't even protect it with double-quoting, though using single
quotes counterintuitively (but expectedly, given the explaination above)
produces the desired result:

zagzig<26> arr['$a']=d
zagzig<27> print -lr - ${(kv)arr}
$x
d
zagzig<28>

I really have no idea when this got introduced.  It might have something
to do with zsh-workers/4826 and follow-ons, back last year; 3.1.5-pws-4
would be it, but I no longer have anything that old around to try.

The right thing would be for the parser to know that it shouldn't expand
inside the [] on the left-hand side of an assignment, thereby delaying
the one interesting expansion to getvalue(); but that may be a bit too
much to bite off ...

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


  reply	other threads:[~1999-09-21  5:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-21  1:37 Tanaka Akira
1999-09-21  4:59 ` Bart Schaefer [this message]
1999-09-28 12:15   ` Peter Stephenson
1999-09-28 13:04     ` Peter Stephenson
1999-09-30 16:58       ` Bart Schaefer

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=990921045934.ZM13380@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=akr@jaist.ac.jp \
    --cc=zsh-workers@sunsite.auc.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).