* using dynamic patterns on the left hand side of case statement clauses
@ 2020-03-18 23:54 scowles
2020-03-19 1:29 ` Lawrence Velázquez
0 siblings, 1 reply; 5+ messages in thread
From: scowles @ 2020-03-18 23:54 UTC (permalink / raw)
To: Zsh hackers list
i would like to use dynamic patterns on the left hand side of clauses in case
statements.
i'm working in zsh current as of 3.1-91-g7595b22e on ubu 19.10.
the options set for this example are:
setopt extended_glob
setopt glob
setopt no_no_match
setopt no_null_glob
the code is:
typeset -a a b c
a=( one two three four )
b=( 16 17 18 19 20 )
c=( two 20 )
vb=$'|'
for d in ${c}
do
case ${d} in
$( eval echo ${(j:${vb}:)a} ) ) echo "1 found it" ;;
$( eval echo ${(j:${vb}:)b} ) ) echo "2 found it" ;;
* ) echo "did not find it" ;;
esac
done
but, when i run the code, the interpreter escapes all the vbars and forces the
entire lhs pattern to be a string.
does the case structure not allow this use case? or am i just missing something
from not reading docs carefully enough?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using dynamic patterns on the left hand side of case statement clauses
2020-03-18 23:54 using dynamic patterns on the left hand side of case statement clauses scowles
@ 2020-03-19 1:29 ` Lawrence Velázquez
2020-03-19 4:38 ` Phil Pennock
0 siblings, 1 reply; 5+ messages in thread
From: Lawrence Velázquez @ 2020-03-19 1:29 UTC (permalink / raw)
To: scowles; +Cc: zsh-workers
> On Mar 18, 2020, at 7:54 PM, scowles@ckhb.org wrote:
>
>
> i would like to use dynamic patterns on the left hand side of clauses
> in case statements.
>
> i'm working in zsh current as of 3.1-91-g7595b22e on ubu 19.10.
>
> the options set for this example are:
> setopt extended_glob
> setopt glob
> setopt no_no_match
> setopt no_null_glob
>
> the code is:
> typeset -a a b c
> a=( one two three four )
> b=( 16 17 18 19 20 )
> c=( two 20 )
> vb=$'|'
> for d in ${c}
> do
> case ${d} in
> $( eval echo ${(j:${vb}:)a} ) ) echo "1 found it" ;;
> $( eval echo ${(j:${vb}:)b} ) ) echo "2 found it" ;;
> * ) echo "did not find it" ;;
> esac
> done
>
>
> but, when i run the code, the interpreter escapes all the vbars and
> forces the entire lhs pattern to be a string.
>
> does the case structure not allow this use case? or am i just
> missing something from not reading docs carefully enough?
The result of an unquoted command substitution is not eligible for
interpretation as a pattern unless GLOB_SUBST is set. (See the
"COMMAND SUBSTITUTION" section of the zshexpn(1) man page, as well
as the "GLOB_SUBST" entry of the zshoptions(1) man page.) So to
make your code work, you could just add `setopt glob_subst`.
However, you can achieve the same result without the ugly
`$(eval echo BLAH)` contortions:
% zsh --version
zsh 5.8 (x86_64-apple-darwin18.7.0)
% cat /tmp/zsh-case-test
setopt extended_glob no_no_match
a=(one two three four)
b=(16 17 18 19 20)
c=(two 20)
for d in ${c}; do
case ${d} in
${(j:|:)~a} ) echo "1 found it" ;;
${(j:|:)~b} ) echo "2 found it" ;;
* ) echo "did not find it" ;;
esac
done
% zsh -f /tmp/zsh-case-test
1 found it
2 found it
%
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.
vq
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using dynamic patterns on the left hand side of case statement clauses
2020-03-19 1:29 ` Lawrence Velázquez
@ 2020-03-19 4:38 ` Phil Pennock
2020-03-19 5:25 ` Lawrence Velázquez
0 siblings, 1 reply; 5+ messages in thread
From: Phil Pennock @ 2020-03-19 4:38 UTC (permalink / raw)
To: Lawrence Velázquez; +Cc: scowles, zsh-workers
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using dynamic patterns on the left hand side of case statement clauses
2020-03-19 4:38 ` Phil Pennock
@ 2020-03-19 5:25 ` Lawrence Velázquez
2020-03-19 17:47 ` scowles
0 siblings, 1 reply; 5+ messages in thread
From: Lawrence Velázquez @ 2020-03-19 5:25 UTC (permalink / raw)
To: Phil Pennock; +Cc: scowles, zsh-workers
> On Mar 19, 2020, at 12:38 AM, Phil Pennock
> <zsh-workers+phil.pennock@spodhuis.org> wrote:
>
> 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.
Oh whoa, that's neat!
vq
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: using dynamic patterns on the left hand side of case statement clauses
2020-03-19 5:25 ` Lawrence Velázquez
@ 2020-03-19 17:47 ` scowles
0 siblings, 0 replies; 5+ messages in thread
From: scowles @ 2020-03-19 17:47 UTC (permalink / raw)
To: zsh-workers; +Cc: Lawrence Velázquez, Phil Pennock
many thanks to you, both.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-03-19 17:48 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-18 23:54 using dynamic patterns on the left hand side of case statement clauses scowles
2020-03-19 1:29 ` Lawrence Velázquez
2020-03-19 4:38 ` Phil Pennock
2020-03-19 5:25 ` Lawrence Velázquez
2020-03-19 17:47 ` scowles
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).