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

Re: using dynamic patterns on the left hand side of case statement clauses



On 2020-03-18 at 21:29 -0400, Lawrence Velázquez wrote:
> Search the zshexpn(1) man page for "${~spec}" for a more thorough
> explanation, but the '~' basically turns GLOB_SUBST on for just
> that parameter expansion.

Good stuff, but I'll offer up one refinement: you can turn on GLOB_SUBST
just for the joining pattern, without turning it on for the entire
pattern.

    % a=('a*' beta gamma delta)
    % c=('alpha', 'a*' 'apple')
    % for d in $c; do case $d in
        ${(j:|:)~a} ) echo "match in a for ${(q-)d}" ;;
        *) echo "no match for ${(q-)d}" ;;
      esac; done
    match in a for alpha,
    match in a for 'a*'
    match in a for apple
    % for d in $c; do case $d in
        ${(~j:|:)a} ) echo "match in a for ${(q-)d}" ;;
        *) echo "no match for ${(q-)d}" ;;
      esac; done
    no match for alpha,
    match in a for 'a*'
    no match for apple

You can thank Peter Stephenson for adding the support for that, when I
was asking about implementing set manipulation for arbitrary content
using zsh auto-unique arrays, some years ago now.

-Phil



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