bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Indirect access to variables, including arrays (was Re: Compare 2 ar


From: Bill Gradwohl
Subject: Re: Indirect access to variables, including arrays (was Re: Compare 2 arrays.)
Date: Thu, 7 Jun 2012 15:14:57 -0600

On Thu, Jun 7, 2012 at 1:01 AM, Pierre Gaston <address@hidden>wrote:

> Fwiw here is a robust and simple solution for in_:
>
> _in () {
>  local e t
>  t="${2:address@hidden";
>  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:address@hidden"
      local e
      for e in "${!2}"; do
         [[ $1 == "$e" ]] && return 0
      done
   else
      set -- "$1" "${2:address@hidden"
      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

[Prev in Thread] Current Thread [Next in Thread]