Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Array expansion interacts with brace expansion in the wrong order
- X-seq: zsh-users 22781
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: "Zsh Users' List" <zsh-users@xxxxxxx>
- Subject: Re: Array expansion interacts with brace expansion in the wrong order
- Date: Fri, 14 Jul 2017 10:36:59 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=oP9s0zkb/tkKkmRm6Z47U/YB2/ls0bBV8c6xDxI7JfI=; b=po1iZQ/7nquWo28zQd1wrnlvGUGKpyWZHXvXXwxEtZ93e/sgUh+KH9JC+m/Kr26Evj x4LOHn5qGezCw143GTVYjtcp38fz2xZrdWqyCnjo08t1X02EGp9tE8nPIj+UFGNeZQhd fR1MJGCcLzF8sMpdakZMDacj7tyhUIR2/ClP4kOu+JL+oAlgh3WUqLR8bswFPANi00We 6tFSE2+Hc8dPrcSG+MkzuyeizNzPlHz/n8+d5PmIWU6kh4gSId9+C05ecMdqvhjrMzui Q76jds2wK/Q7Qnbsgz0u3W2h9urgZINx06X6CkJtCwVYjD/fWPYCCEHIFW4MkmeP1T/r WEpg==
- In-reply-to: <20170714104158.6f58d200@pwslap01u.europe.root.pri>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <alpine.DEB.2.10.1707131831290.28302@mass-toolpike.mit.edu> <CAH+w=7bzJx11_LKOtJz0BwVUQM_rU+d2nykybcGhVTMoi6Uj3w@mail.gmail.com> <CGME20170714014844epcas4p2d24ea25d43338e3fa7097e5bb01d3fac@epcas4p2.samsung.com> <alpine.DEB.2.10.1707132128530.28302@mass-toolpike.mit.edu> <20170714094831.576c3d79@pwslap01u.europe.root.pri> <20170714104158.6f58d200@pwslap01u.europe.root.pri>
On Fri, Jul 14, 2017 at 2:41 AM, Peter Stephenson
<p.stephenson@xxxxxxxxxxx> wrote:
>
> The problem is that
>
> a=(1 2 3)
> print {$^a,4,5,6}
>
> doesn't expand $^a at the same time as the braces, because all array
> expansion comes before all brace expansion. So how do you get 1 2 3 4 5
> 6 treated as a set of array elements without actually constructing an
> array?
That's not precisely the question, is it? If it were, the answer
would simply be to write
$a 4 5 6
The question is how to get that to behave like a brace expansion with
respect to the surrounding text.
> If you're not too fussy about embedded spaces, you can do
>
> % print x${^=:-$a 4 5 6}y
> x1y x2y x3y x4y x5y x6y
>
> I haven't come up with a way that allows you effectively to extend the
> array without using the hack of splitting up strings.
The root of the problem is that both $^a and {4,5,6} do lexical
catenation with the surrounding text (which, in the absence of word
splitting, is consistent with how the shell language always catenates
adjacent tokens), but the problem requires what might be called
semantic catenation (appending two arrays results in one longer array
without altering individual elements).
This handles embedded spaces in $a better:
print -l x${^:-$a${=:- 4 5 6}}y
(note, the space between =:- and 4 is important, you need an extra
empty element to catenate with $a)
Another way to deal with embedded spaces in elements of $a:
% print x${(z)^:-${(q)a} 4 5 6}y
The "squiggles" there mean
- (z) splits the expansion as if parsing a command line
- ^ ensures that array expansion behaves like brace expansion.
- :- says "use the following if there's no parameter string yet".
- (q) quotes the elements [later unquoted by (z)]
Messages sorted by:
Reverse Date,
Date,
Thread,
Author