* Brace expansion as array literals, bug or feature?
@ 2015-05-31 5:15 Mikael Magnusson
2015-05-31 8:17 ` Bart Schaefer
0 siblings, 1 reply; 2+ messages in thread
From: Mikael Magnusson @ 2015-05-31 5:15 UTC (permalink / raw)
To: Zsh Users
You can do this
% print -rl - ${(Oa):-{a,b,c}}
c
b
a
But not this
% print -rl - "${(Oa):-{a,b,c}}"
{a,b,c}
I can't really recall seeing any statement in the manpage that either
of these ought to work. The first one does not simply expand to three
parameter substitutions; if it did, the order would not be reversed
over the whole array by (Oa).
This is clearly a ridiculous workaround,
% print -rl - ${${(A)reply::={a,b,,c}}+} "${${(@Oa)reply}}"
c b a
Another nice thing is you don't need to quote spaces in this form :)
% print -l {a,b b,c}
zsh: parse error near `}'
% print -l ${:-{a,b b,c}}
a
b b
c
Before discovering this, I usually did something like ${(s:,:):-a,b,c}
but that feels somehow more hacky. Is there another way to do an array
literal that I'm not thinking of?
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Brace expansion as array literals, bug or feature?
2015-05-31 5:15 Brace expansion as array literals, bug or feature? Mikael Magnusson
@ 2015-05-31 8:17 ` Bart Schaefer
0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2015-05-31 8:17 UTC (permalink / raw)
To: Zsh Users
On May 31, 7:15am, Mikael Magnusson wrote:
} Subject: Brace expansion as array literals, bug or feature?
}
} You can do this
} % print -rl - ${(Oa):-{a,b,c}}
} c
} b
} a
You can also do this:
x=(a b c)
print -rl - ${(Oa):-$x}
Indeed it's not spelled out anywhere, but in ${NAME:-WORD} if WORD would
result in an array, then the substitution results in an array. Perhaps
the "Rules" section should mention at which stage :- :+ etc. take place.
Whether ${(Oa):-{a,b,c}} is a bug is unclear. Bash does not perform
brace expansion at that point because ${...} is effectively a form of
quoting.
} But not this
} % print -rl - "${(Oa):-{a,b,c}}"
} {a,b,c}
Yes, quoting the whole expression puts WORD in a quoted context, so e.g.
in my $x example you need
print -rl - "${(Oa):-$x[@]}"
There's nothing to correspond to an [@] subscript for brace-expansion.
} This is clearly a ridiculous workaround,
} % print -rl - ${${(A)reply::={a,b,,c}}+} "${${(@Oa)reply}}"
} c b a
Somewhat less ridiculous:
print -rl - ${(Qj: :)${(qOa):-{a,b,,c}}}
} Another nice thing is you don't need to quote spaces in this form :)
The spaces are quoted by ${...}, which is another point toward id being
a bug that the braces are not also quoted.
} Before discovering this, I usually did something like ${(s:,:):-a,b,c}
} but that feels somehow more hacky. Is there another way to do an array
} literal that I'm not thinking of?
I can't think of another one either.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-05-31 8:17 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-31 5:15 Brace expansion as array literals, bug or feature? Mikael Magnusson
2015-05-31 8:17 ` 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).