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 22780
- From: Peter Stephenson <p.stephenson@xxxxxxxxxxx>
- 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:41:58 +0100
- Cms-type: 201P
- In-reply-to: <20170714094831.576c3d79@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
- Organization: Samsung Cambridge Solution Centre
- 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>
On Fri, 14 Jul 2017 09:48:31 +0100
Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> I can't think of an easy workaround for
> your case except by constructing an array
>
> b=($a 4 5 6)
>
> which is obviously rather more verbose.
Hmm... we're now in zsh-users territory, so I'll summarise.
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?
Because of expansion ordering, the trick is to make sure it looks purely
like an array expansion --- if you leave any of it to brace expansion,
it'll expand recursively.
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
(x and y demonstrate the fact that we have indeed got 6 different
elements).
All the squiggles are necessary:
- ${...} creates the array expansion we're going to use to simulate
brace expansion.
- ^ ensures that array expansion behaves like brace expansion.
- = splits the string "$a 4 5 6" on spaces. Things will go awry here
if there are embedded spaces.
- :- says "use the following if there's no parameter string yet" which
there isn't.
- Then follows a simple string to be split.
I haven't come up with a way that allows you effectively to extend the
array without using the hack of splitting up strings.
pws
Messages sorted by:
Reverse Date,
Date,
Thread,
Author