bug-bash
[Top][All Lists]
Advanced

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

Re: currently doable? Indirect notation used w/a hash


From: Linda Walsh
Subject: Re: currently doable? Indirect notation used w/a hash
Date: Sat, 15 Jun 2013 12:36:22 -0700
User-agent: Thunderbird



John Kearney wrote:
Sorry forgot the bit to retrive values
It is possible to retrive numeric values without eval
i.e.
val=$((${ArrayName}[Index]))
works quiet well and is quick, in fact I used to use this quiet a lot.
There is also a backdoor approach that I don't really advise.
val="${ArrayName}[Index]"
echo "${!val}"
-----
        Don't advise?  Any particular reason?  or stylistic?

Some interesting ways of doing these things -- will have to play
around with them to see howuseful they are in place of some of my
existing hacks -- see if they help them look cleaner ... am always
looking for new ways to clean up my code and make it simpler/easier
to understand even it looks non-standard to language purists...

thanks!


What I actually tend to do is.
  ks_array_ChkName() {
    local LC_COLLATE=C
    case "${1:?Missing Variable Name}" in
      [!a-zA-Z_]* | *[!][a-zA-Z_0-9]* ) return 3;;
    esac
  }
  ks_val_Get() {
    ks_array_ChkName "${1:?Missing Destination Variable Name}" || return $?
    ks_array_ChkName "${2:?Missing Source Variable Name}" || return $?
    eval "${1}=\"\${${2}:-}\""
  }
  ks_array_GetVal() { ks_val_Get "${1}" "${2}[${3}]" ; }
  ks_array_SetVal() { ks_val_Set "${1}[${2}]" "${3:-}" ; }
}
ks_array_SetVal() {
ks_val_ChkName "${1:?Missing Array Name}" || return $?
ks_val_ChkName "a${2:?Missing Array Index}" || return $?
eval "${1}"["${2}"]'="${3:-}"'
}
ks_array_SetVal "${ArrayName}" "Index" "Value"




reply via email to

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