From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=0.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE,RDNS_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 Received: (qmail 20509 invoked from network); 19 Mar 2020 01:30:46 -0000 Received-SPF: pass (primenet.com.au: domain of zsh.org designates 203.24.36.2 as permitted sender) receiver=inbox.vuxu.org; client-ip=203.24.36.2 envelope-from= Received: from unknown (HELO primenet.com.au) (203.24.36.2) by inbox.vuxu.org with ESMTP; 19 Mar 2020 01:30:46 -0000 Received: (qmail 7224 invoked by alias); 19 Mar 2020 01:30:33 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 45578 Received: (qmail 27660 invoked by uid 1010); 19 Mar 2020 01:30:33 -0000 X-Qmail-Scanner-Diagnostics: from out4-smtp.messagingengine.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.2/25751. spamassassin: 3.4.2. Clear:RC:0(66.111.4.28):SA:0(-2.7/5.0):. Processed in 2.767265 secs); 19 Mar 2020 01:30:33 -0000 X-Envelope-From: vq@larryv.me X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at spf.messagingengine.com designates 66.111.4.28 as permitted sender) X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrudefkedgfeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpegtggfuhfgjfffgkfhfvffosehtje hmtdhhtddvnecuhfhrohhmpefnrgifrhgvnhgtvggpgggvlhojiihquhgviicuoehvqhes lhgrrhhrhihvrdhmvgeqnecukfhppedutddtrdduvddrkedurdduheefnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepvhhqsehlrghrrhihvhdr mhgv X-ME-Proxy: Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: using dynamic patterns on the left hand side of case statement clauses From: =?utf-8?Q?Lawrence_Vel=C3=A1zquez?= In-Reply-To: Date: Wed, 18 Mar 2020 21:29:54 -0400 Cc: zsh-workers@zsh.org Content-Transfer-Encoding: 7bit Message-Id: References: To: scowles@ckhb.org X-Mailer: Apple Mail (2.3445.104.11) > 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