ksh93 中的函数和关联数组混乱
这是我的简单数组:
typeset -A foo
foo["first"]="first Value"
foo["second"]="second Value"
我想做一个函数来选择这个数组,做一些事情并将其返回给脚本。例如,
function changeThat {
eval tmp=\$$1
tmp["$2"]=$3
return $tmp
}
我可以在脚本中执行以下操作:
foo=changeThat foo "first" "a new first value"
并得到一个漂亮的结果,例如
echo ${foo["first"]}
a new first value
现在这不起作用...好吧,我知道语法可能不太正确。但我真的迷失了 evals
和 scape echo
的细微差别(并不是说我从灵魂深处讨厌它)。此外,我的参考是 bash 并且这不是我第一次错过涉及 ksh 时有一些技巧 - 例如,到目前为止我一直在使用 ksh88
,它甚至没有关联数组,而大多数人说它应该有。结果我的AIX机器不同意。 -_-
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样定义函数:
typeset -n ref
将 ref 变量定义为对其值指定的变量的引用。当您对函数进行此调用时:
函数changeThat 中的变量ref 引用变量foo。现在使用 ref 就像使用 foo 一样。调用changeThat后,
现在将输出“修剪草坪”。
You can define your function like this:
typeset -n ref
defines 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".