Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: PATCH: param stuff and was: PATCH: 3.1.5-pws-12: _brace_parameter



"Andrej Borsenkow" wrote:
> bor@itsrm2:~%> foo=(bar baz)
> bor@itsrm2:~%> print "${(@)${foo}[1]}"
> bar baz
> bor@itsrm2:~%> print "${${(@)foo}[1]}"
> b
> 
> Could anybody explain, why?

It's actually completely predictable, once you know the rule.

In the first case the processing is
 - "${foo}" giving quoted substituion, the word "bar baz"
 - "${(@)...[1]}" where the ... is the result of the above.  This
   does array indexing on what you have already, because of the (@).
   What you have already is a single word, so that is the first word of the
   array and you get that.

In the second case, you get
 - "${(@)foo}", giving the array (bar baz)
 - "${...[1]}" on the result of that.  There is no (@), so this is
   a scalar substitution, so what we have so far is turned into a single
   word and the [1] applies to characters in that, giving the `b'.

The rule is:

  Substitution at each level only takes account of its own flags and
  whether or not it is in double quotes in deciding how to process
  what it has received from any nested substituion.  It knows whether
  or not that was an array, in case it needs to join the words or
  select one or more of them, but it has no way of knowing about nested
  flags and whether it should propagate them.

Maybe I can even invent something for the manual, if it's not going to
change.

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxx>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy



Messages sorted by: Reverse Date, Date, Thread, Author