zsh-users
 help / color / mirror / code / Atom feed
* Re: Array expansion interacts with brace expansion in the wrong order
       [not found]       ` <20170714094831.576c3d79@pwslap01u.europe.root.pri>
@ 2017-07-14  9:41         ` Peter Stephenson
  2017-07-14 17:36           ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2017-07-14  9:41 UTC (permalink / raw)
  To: Zsh Users' List

On Fri, 14 Jul 2017 09:48:31 +0100
Peter Stephenson <p.stephenson@samsung.com> 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Array expansion interacts with brace expansion in the wrong order
  2017-07-14  9:41         ` Array expansion interacts with brace expansion in the wrong order Peter Stephenson
@ 2017-07-14 17:36           ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2017-07-14 17:36 UTC (permalink / raw)
  To: Zsh Users' List

On Fri, Jul 14, 2017 at 2:41 AM, Peter Stephenson
<p.stephenson@samsung.com> 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)]


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-07-14 17:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <alpine.DEB.2.10.1707131831290.28302@mass-toolpike.mit.edu>
     [not found] ` <CAH+w=7bzJx11_LKOtJz0BwVUQM_rU+d2nykybcGhVTMoi6Uj3w@mail.gmail.com>
     [not found]   ` <CGME20170714014844epcas4p2d24ea25d43338e3fa7097e5bb01d3fac@epcas4p2.samsung.com>
     [not found]     ` <alpine.DEB.2.10.1707132128530.28302@mass-toolpike.mit.edu>
     [not found]       ` <20170714094831.576c3d79@pwslap01u.europe.root.pri>
2017-07-14  9:41         ` Array expansion interacts with brace expansion in the wrong order Peter Stephenson
2017-07-14 17:36           ` Bart Schaefer

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).