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

Re: (s) splitting – is there any way to provide «dynamic» separator

On 9 October 2014 23:14, Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On Thu, 09 Oct 2014 21:00:32 +0600
> Vasiliy Ivanov <beelzebubbie.logs@xxxxxxxxx> wrote:
>> Sorry if I (maybe) missed something obvious, but I failed to find a way to use separator from
>> parameter (e.g. a='1:2:3'; sep=':'; print -l ${(s.$sep.)a}).
>> I think this is because expansion/substitution are not available in
>> flags? If so, is there any  another way for such a «dynamic» splitting?
> I don't think you've missed anything obvious or elegant.
> One way is
> (){
>   local IFS=$sep
>   print -l ${=a}
> }
> Or if you can rely on not having whitespace
> print -l ${=${a//:/ }}
> Or, of course,
> eval print -l '${(s.'$sep'.)a}'
> as long as you can sanity check $sep.
> Of course there's nothing to stop us adding a simple parameter
> substitution as a special case at this point (or any similarly delimited
> parameter argument) ...  Can't see how this can do much harm since it
> sure as heck *looks* like a parameter expansion.

I checked to make sure you can still do (s:$:) and that does still
work. Moving into extremely unlikely corner-case land, (s:$b:) would
split on the literal string $b before. With the patch, it actually
still does, but only if b is unset. If b is set, then it is expanded
and the expansion is split on instead. So the only problem here would
be if someone used to split on a literal string $foo and then foo is
set, their code would stop working.

I don't know if it's worth it but we could possibly elect to only do
this expansion if the p flag is active? Right now it means to expand
\t into a literal tab, but it makes sense to have it more generally
mean "expand stuff" in the string I think?

eg, I think this would do it?
+    if (escapes && (*s == String || *s == Qstring) && s[1]) {

Mikael Magnusson

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