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"