bug-bash
[Top][All Lists]
Advanced

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

Re: question on retrieving map(-A) value w/indirect name


From: David Margerison
Subject: Re: question on retrieving map(-A) value w/indirect name
Date: Sat, 17 Mar 2018 23:05:57 +1100

On 17 March 2018 at 20:40, L A Walsh <bash@tlinx.org> wrote:
>
> I see....but that begs the question, how do you access an array's
> members using a var holding the array's name?
>
> I wanted to be able to do something like have a set of
> values in an assoc. map, and pass the name to a generic
> processing routine with the map name as a param, like:
>
> sub processSrvState() {
>    my stat=${1:?}
>    if [[ ${!stat[cur_up]} == ${!stat[max_up]} &&
>      ${!stat[cur_down]} == ${!stat[max_down]} ]]; then
>      ...
>    fi

processSrvState() {
  local cur_up="$1[cur_up]"
  local max_up="$1[max_up]"
  if [[ "${!cur_up}" == "${!max_up}" ]] ; then
    echo ok
  fi
}

declare -A foo=([cur_up]=11 [max_up]=11)

processSrvState foo

# note that the array name must not conflict with any keys



reply via email to

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