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

Re: Brace expansion as array literals, bug or feature?



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.



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