从任意/命名的关联数组中提取值以及其中的键。

huangapple go评论52阅读模式
英文:

Extracting values from arbitrary/named associative arrays and keys within them

问题

我使用 declare -A 来添加字典中的键值对。

APP_LIST=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")

我尝试使用循环来打印从 app1 和 app2 中的 column_b 的值,但似乎不起作用。

for i in ${APP_LIST[*]}; do
    echo ${${i}[column_b]}
done

错误消息:

${${i}[column_b]}: bad substitution

请帮忙,谢谢!

我期望使用循环来打印 app1 和 app2 中的 column_b 的值。

英文:

I use declare -A to add key-value pairs in dictionary.

APP_LIST=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")

I have tried to print the value of the column_b from app1 and app2 using for-loop, but it doesn't seem to work.

for i in ${APP_LIST[*]}; do
    echo ${${i}[column_b]}
done

The error msg:

 ${${i}[column_b]}: bad substitution

Please help, thanks!

I expected to print the value of the column_b from app1 and app2 using for-loop.

答案1

得分: 0

以下是您要翻译的内容:

$ cat tst.sh
#!/usr/bin/env bash

app_list=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")

declare -n arr_name
for arr_name in "${app_list[@]}"; do
    echo "${arr_name[column_b]}"
done
$ ./tst.sh
222
444
英文:
$ cat tst.sh
#!/usr/bin/env bash

app_list=('app1' 'app2')
declare -A app1=([column_a]="111" [column_b]="222")
declare -A app2=([column_a]="333" [column_b]="444")

declare -n arr_name
for arr_name in "${app_list[@]}"; do
    echo "${arr_name[column_b]}"
done

<p>

$ ./tst.sh
222
444

huangapple
  • 本文由 发表于 2023年5月22日 19:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定