This is my simple array:
typeset -A foo
foo["first"]="first Value"
foo["second"]="second Value"
And I want to do a function that would pick this array, do something and return it back to the script. e.g.
function changeThat {
eval tmp=\$$1
tmp["$2"]=$3
return $tmp
}
I a way could go along in the script and do something like:
foo=changeThat foo "first" "a new first value"
And get a pretty result like
echo ${foo["first"]}
a new first value
Now this doesn’t work… Well, I’m aware the syntax is prob not quite right. But I got really lost going through the nuances of evals and scape echo (not to say that I hate it from the bottom of my soul). Besides, my reference is for bash and wouldn’t be the first time I miss some trick when it comes to ksh – For instance, I’ve been so far in ksh88, which does’t even have associative arrays, while most people say it should. Turns out that my AIX box does not agree. -_-
thanks!
You can define your function like this:
typeset -n refdefines the ref variable as a reference to the variable specified by it’s value.When you make this call to the function:
The variable ref in function changeThat references the variable foo. Using ref is now just like using foo. After calling changeThat
will now output “mow the lawn”.