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

Re: The ~ parameter expansion flag: bug or misunderstanding

On Wed, 03 Sep 2014 10:58:23 -0400
Clint Hepner <clint.hepner@xxxxxxxxx> wrote:

> I understand that you can split a parameter value on a fixed string:
> % print -l ${(s.1.):-a1b1c}
> a
> b
> c
> My reading of the ~ flag leads me to believe that you can replace the
> literal string with a pattern, so that
> % print -l ${(~s.[12].):-a1b2c}
> a
> b
> c
> However, the ~ flag seems to have no effect, with the parameter string
> remaining unsplit.

No, splitting is with (s.X.) is always on a string.

You can do something like

print -l ${(s.1.)${${:-a1b2c}//[12]/1}}

The documentation isn't very clear:

~      Force string arguments to any of the  flags  below  that  follow
       within  the parentheses to be treated as patterns.  Compare with
       a ~ outside parentheses, which  forces  the  entire  substituted
       string to be treated as a pattern.  Hence, for example,
       [[ "?" = ${(~j.|.)array} ]]

actually means string arguments remaining after substitution are treated
as a pattern subseqeuently on the command line --- which means that it
has no effect on splitting, despite "s" being one of the flags below.
So it doesn't really say what it does in this case.

Peter Stephenson <p.stephenson@xxxxxxxxxxx>  Principal Software Engineer
Tel: +44 (0)1223 434724                Samsung Cambridge Solution Centre
St John's House, St John's Innovation Park, Cowley Road,
Cambridge, CB4 0DS, UK

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