On Thu, Jun 7, 2012 at 1:01 AM, Pierre Gaston <pierre.gas...@gmail.com>wrote:

> Fwiw here is a robust and simple solution for in_:
>
> _in () {
>  local e t
>  t="${2:?}[@]";
>  for e in "${!t}"; do [[ $1 = "$e" ]] && return 0;done
>  return 1;
> }
>
>
The following won't have a name space collision with the 'e' variable and I
got rid of 't'.

_in () {
   if [ "$2" != 'e' ]; then
      set -- "$1" "${2:?}[@]"
      local e
      for e in "${!2}"; do
         [[ $1 == "$e" ]] && return 0
      done
   else
      set -- "$1" "${2:?}[@]"
      local _e
      for _e in "${!2}"; do
         [[ $1 == "$_e" ]] && return 0
      done
   fi

   return 1
}

array=(a b c abc d e f)
_in abc array  && echo Yes || echo No

array=(a b c abxc d e f)
_in abc array  && echo Yes || echo No

e=(a b c abc d e f)
_in abc e  && echo Yes || echo No

e=(a b c abxc d e f)
_in abc e  && echo Yes || echo No


-- 
Bill Gradwohl

Reply via email to