击:如何关联数组赋值给另一个变量的名称(例如重命名变量)? [英] Bash: How to assign an associative array to another variable name (e.g. rename the variable)?

查看:128
本文介绍了击:如何关联数组赋值给另一个变量的名称(例如重命名变量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要循环关联数组和漏它的内容到一个临时阵列(并执行一些更新的值)。

第一个数组的剩余内容应该再被抛弃,我想临时数组原始数组变量分配。

须藤code:

 声明-A MAINARRAY
声明-A TEMPARRAY
...填充$ {MAINARRAY [...]} ...而一些;从MAINARRAY做#Drain一些值TEMPARRAY
    $ {TEMPARRAY [$名]} =(($ {MAINARRAY [$名]} + $ someValue中))
DONE
...其他操作来TEMPARRAY ...未设置MAINARRAY #discard遗留下来没有更新值
声明-A MAINARRAY
MAINARRAY = $ {TEMPARRAY [@]} #assign更新TEMPARRAY回MAINARRAY(ERROR HERE)


解决方案

通过关联数组,我不相信有任何其他方法比迭代

 在关键的$ {!TEMPARRAY [@]}#确保包含引号有

  MAINARRAY [$键] =$ {TEMPARRAY [$键]}
  #或:MAINARRAY + =([$键] =$ {TEMPARRAY [$键]})
DONE

I need to loop over an associative array and drain the contents of it to a temp array (and perform some update to the value).

The leftover contents of the first array should then be discarded and i want to assign the temp array to the original array variable.

Sudo code:

declare -A MAINARRAY
declare -A TEMPARRAY
... populate ${MAINARRAY[...]} ...

while something; do     #Drain some values from MAINARRAY to TEMPARRAY
    ${TEMPARRAY["$name"]}=((${MAINARRAY["$name"]} + $somevalue))
done
... other manipulations to TEMPARRAY ...

unset MAINARRAY        #discard left over values that had no update
declare -A MAINARRAY
MAINARRAY=${TEMPARRAY[@]}  #assign updated TEMPARRAY back to MAINARRAY (ERROR HERE)

解决方案

With associative arrays, I don't believe there's any other method than iterating

for key in "${!TEMPARRAY[@]}"  # make sure you include the quotes there
do
  MAINARRAY["$key"]="${TEMPARRAY["$key"]}"
  # or: MAINARRAY+=( ["$key"]="${TEMPARRAY["$key"]}" )
done

这篇关于击:如何关联数组赋值给另一个变量的名称(例如重命名变量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆