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

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



On 30 May, Frank Terbeck wrote:
> Helmut Jarausch <jarausch@xxxxxxxxxxxxxxxxxxx>:
>> please help me with the following:
>> 
>> Is there an elegant way (i.e. without a loop) to
>> get the following action
>> 
>> 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

if [[ -n ${(M)Special_Users:#${U}} ]] ; then
    echo $U is in there - Version 1
else
    echo not found - Version 1
fi

if (($Special_Users[(Ie)$U])); then
    echo $U is in there - Version 2
else
    echo not found - Version 2
fi

# output:

# 4.3.6-dev-0
# not found - Version 1
# not found - Version 2


Many thanks for enlightening me,
Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany



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