From: Phil Pennock <Phil.Pennock@globnix.org>
To: zsh-users@sunsite.dk
Subject: case matching to element in array?
Date: Thu, 5 Dec 2002 14:31:33 +0100 [thread overview]
Message-ID: <20021205143133.A29978@globnix.org> (raw)
I'm probably missing something really obvious here. :^/ zsh-4.0.6.
As part of a shell function, I need to have one case occur if the
parameter matches an element in an array variable. This can trivially
be done with if/else/endif.
I'm actually extending an existing function, which already uses "case"
and has several other situations handled. Changing a dispatch-on-value
from "case" to "if" feels unclean. I'll use an "if" for now, since I
need to get this done quickly, but I'm hoping there's a better method.
What am I missing in the following example?
-----------------------------< cut here >-------------------------------
function check {
typeset -aU fred_list
fred_list=(alpha beta gamma delta)
case ${(L)1} in
(foo)
print Foo
;;
(${(j:|:)fred_list})
print In List of Fred
;;
(*)
print Default
;;
esac
}
% check foO
Foo
% check asdf
Default
% check beta
Default
-----------------------------< cut here >-------------------------------
Is there any way to construct the pattern for a case-match from a
variable? Or is the pattern constructed, but the '|' splitting done
first, so that I'm ending up with a pattern with '|' in it?
If there's not a current method of doing this cleanly, then is it a
reasonable request to ask that the () matches in "case" statements be
extended to allow array contents, with an implicit '|' between the
elements of the array? This would have the advantage of providing an
easy way to match on a string containing a '|', since you could do:
set -A foo 'a|b'
case $bar in
($foo) print ni ;;
esac
which would avoid any need for special escaping.
At least, in the docs which I'm reading now, array-contents aren't
mentioned as a possibility and it doesn't seem to "work" as I describe.
If I've missed something, please enlighten me.
Thanks,
--
"We've got a patent on the conquering of a country through the use of force.
We believe in world peace through extortionate license fees." -Bluemeat
next reply other threads:[~2002-12-05 13:31 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-05 13:31 Phil Pennock [this message]
2002-12-05 13:42 ` Peter Stephenson
2002-12-05 14:18 ` Peter Stephenson
2002-12-05 17:38 ` Bart Schaefer
2002-12-05 17:42 ` Peter Stephenson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20021205143133.A29978@globnix.org \
--to=phil.pennock@globnix.org \
--cc=zsh-users@sunsite.dk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).