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

* Re: case matching to element in array?
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2002-12-05 13:42 UTC (permalink / raw)
  To: zsh-users

Phil Pennock wrote:
> What am I missing in the following example?
> 
> 		(${(j:|:)fred_list})

Change this to

                (${~${(j:|:)fred_list}})

to activate the `|'s.  The extra level of substitution seems to be
necessary to avoid the `|' in the (j:|:) having an effect too soon.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Re: case matching to element in array?
  2002-12-05 13:42 ` Peter Stephenson
@ 2002-12-05 14:18   ` Peter Stephenson
  2002-12-05 17:38     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2002-12-05 14:18 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote:
> Phil Pennock wrote:
> > What am I missing in the following example?
> > 
> > 		(${(j:|:)fred_list})
> 
> Change this to
> 
>                 (${~${(j:|:)fred_list}})
> 
> to activate the `|'s.  The extra level of substitution seems to be
> necessary to avoid the `|' in the (j:|:) having an effect too soon.

I worked out why I got confused before:  the canonical form is

                  (${(j:|:)~fred_list})

and I was putting the ~ before the (j:|:), which is why it wasn't
working.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* Re: case matching to element in array?
  2002-12-05 14:18   ` Peter Stephenson
@ 2002-12-05 17:38     ` Bart Schaefer
  2002-12-05 17:42       ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2002-12-05 17:38 UTC (permalink / raw)
  To: Peter Stephenson, zsh-users

On Dec 5,  2:18pm, Peter Stephenson wrote:
} Subject: Re: case matching to element in array?
}
} I worked out why I got confused before:  the canonical form is
} 
}                   (${(j:|:)~fred_list})
} 
} and I was putting the ~ before the (j:|:), which is why it wasn't
} working.

Is there some reason why putting the tilde INSIDE the parens could not
be made to work as well?

    ${(j:|:~)fred_list} == ${(~j:|:)fred_list} == ${(j:|:)~fred_list}


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

* Re: case matching to element in array?
  2002-12-05 17:38     ` Bart Schaefer
@ 2002-12-05 17:42       ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2002-12-05 17:42 UTC (permalink / raw)
  To: zsh-users

"Bart Schaefer" wrote:
> Is there some reason why putting the tilde INSIDE the parens could not
> be made to work as well?
> 
>     ${(j:|:~)fred_list} == ${(~j:|:)fred_list} == ${(j:|:)~fred_list}

Should be trivial to implement.  It's a historical oddity that `^', `~'
and `=' came before we had far more flags than could be accommodated
without some special syntax to mark them.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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