关联数组通过字符串初始化且无需声明。

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

associative array initialization via string and without declare

问题

I want to have a global associative array, that is filled at several locations and I can not get it to work to initialize the array with the content of a string without using the declare -A -g over and over at said locations(which I don't feel like is the smartest approach). I've extracted the issue to the code below:

  1. #!/bin/bash
  2. declare -A arr
  3. declare inputString
  4. inputString="([key]=val)"
  5. arr="$inputString"
  6. printf "from variable: ${arr[*]}\n"
  7. printf "from variable: ${arr[key]}\n"
  8. arr=([key]=val)
  9. printf "inline: ${arr[*]}\n"
  10. printf "inline: ${arr[key]}\n"
  11. declare -A arr=$inputString
  12. printf "with declare: ${arr[*]}\n"
  13. printf "with declare: ${arr[key]}\n"

the output is:

  1. from variable: ([key]=val)
  2. from variable:
  3. inline: val
  4. inline: val
  5. with declare: val
  6. with declare: val

whereas I would expect it to be:

  1. from variable: val
  2. from variable: val
  3. inline: val
  4. inline: val
  5. with declare: val
  6. with declare: val

what do I have to do to accomplish this task?

英文:

I want to have a global associative array, that is filled at several locations and I can not get it to work to initialize the array with the content of a string without using the declare -A -g over and over at said locations(which I don't feel like is the smartest approach). I've extracted the issue to the code below:

  1. #!/bin/bash
  2. declare -A arr
  3. declare inputString
  4. inputString="([key]=val)"
  5. arr="$inputString"
  6. printf "from variable: ${arr[*]}\n"
  7. printf "from variable: ${arr[key]}\n"
  8. arr=([key]=val)
  9. printf "inline: ${arr[*]}\n"
  10. printf "inline: ${arr[key]}\n"
  11. declare -A arr=$inputString
  12. printf "with declare: ${arr[*]}\n"
  13. printf "with declare: ${arr[key]}\n"

the output is:

  1. from variable: ([key]=val)
  2. from variable:
  3. inline: val
  4. inline: val
  5. with declare: val
  6. with declare: val

whereas I would expect it to be:

  1. from variable: val
  2. from variable: val
  3. inline: val
  4. inline: val
  5. with declare: val
  6. with declare: val

what do I have to do to accomplish this task?

答案1

得分: 4

以下是翻译好的内容:

One bash 4.3+ approach using a nameref:

  1. myadd() { # $1 == array name; $2 == key name; $3 == value
  2. declare -n _arr="$1" # nameref
  3. _arr+=([$2]="$3")
  4. }
  5. myclear() {
  6. declare -n _arr="$1"
  7. _arr=()
  8. }
  9. Taking for a test drive:
  10. unset arr1 arr2
  11. declare -A arr1 arr2
  12. myadd arr1 keyX 3
  13. myadd arr1 Index_45 16
  14. myadd arr2 long_string_abc long_value_xyz
  15. typeset -p arr1 arr2
  16. ==> declare -A arr1=([keyX]="3" [Index_45]="16" )
  17. ==> declare -A arr2=([long_string_abc]="long_value_xyz" )
  18. myclear arr1
  19. myadd arr1 aaa 111
  20. myadd arr1 bbb 222
  21. typeset -p arr1
  22. ==> declare -A arr1=([bbb]="222" [aaa]="111" )
英文:

One bash 4.3+ approach using a nameref:

  1. myadd() { # $1 == array name; $2 == key name; $3 == value
  2. declare -n _arr="$1" # nameref
  3. _arr+=(["$2"]="$3")
  4. }
  5. myclear() {
  6. declare -n _arr="$1"
  7. _arr=()
  8. }

Taking for a test drive:

  1. unset arr1 arr2
  2. declare -A arr1 arr2
  3. myadd arr1 keyX 3
  4. myadd arr1 Index_45 16
  5. myadd arr2 long_string_abc long_value_xyz
  6. typeset -p arr1 arr2
  7. ==> declare -A arr1=([keyX]="3" [Index_45]="16" )
  8. ==> declare -A arr2=([long_string_abc]="long_value_xyz" )
  9. myclear arr1
  10. myadd arr1 aaa 111
  11. myadd arr1 bbb 222
  12. typeset -p arr1
  13. ==> declare -A arr1=([bbb]="222" [aaa]="111" )

huangapple
  • 本文由 发表于 2023年4月13日 21:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006149.html
匿名

发表评论

匿名网友

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

确定