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

Re: Check existence of an element of an array (a FAQ?)



Helmut Jarausch <jarausch@xxxxxxxxxxxxxxxxxxx>:
> On 30 May, Frank Terbeck wrote:
> > Helmut Jarausch <jarausch@xxxxxxxxxxxxxxxxxxx>:
[...]
> >> Special_Users=('you','me',......)
> >> 
> >> U='nobody'
> >> 
> >> if  $U in $Special_Users
> >>   # do something
> >> fi
> > 
> > [snip]
> > if [[ -n ${(M)Special_Users:#${U}} ]] ; then
> >     echo $U is in there
> > fi
> > [snap]
[...]
> Sorry, but I seem to have missed something.
> I tried both methods without success.
> 
> Here is my script and the output.
> (It's ZSH CVS from today)
> 
> #!/bin/zsh
> echo $ZSH_VERSION
> Special_Users=('you','me')
> U='me'
> # same with 
> # Special_Users=(you,me)
> # U=me
[...]

Both versions will work, if you construct the array correctly.

Both of your attempts to create the array with two items 'me' and
'your' will result in *one* item with the value 'you,me'. Therefore,
both implementations do not find 'me'. Simply because it is not there.

Try:    Special_Users=( you me )

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925



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