zsh-users
 help / color / mirror / code / Atom feed
* case matching to element in array?
@ 2002-12-05 13:31 Phil Pennock
  2002-12-05 13:42 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Phil Pennock @ 2002-12-05 13:31 UTC (permalink / raw)
  To: zsh-users

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-12-05 17:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-05 13:31 case matching to element in array? Phil Pennock
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

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).