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

case matching to element in array?

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
			print Foo
			print In List of Fred
			print Default

% check foO
% check asdf
% check beta
-----------------------------< 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 ;;

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.

"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

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