Bash 中的多维关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6149679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:33:51  来源:igfitidea点击:

Multidimensional associative arrays in Bash

bashhashmultidimensional-arrayhashtableassociative-array

提问by Max

I'm trying to create a multidimensional associative array but need some help. I have reviewed the page suggested in this SO answerbut it confused me even more. So far here is what I have:

我正在尝试创建一个多维关联数组,但需要一些帮助。我已经查看了这个 SO 答案中建议的页面,但它让我更加困惑。到目前为止,这是我所拥有的:

The script:

剧本:

#!/bin/bash
declare -A PERSONS
declare -A PERSON
PERSON["FNAME"]='John'
PERSON["LNAME"]='Andrew'
PERSONS["1"]=${PERSON[@]}
PERSON["FNAME"]='Elen'
PERSON["LNAME"]='Murray'
PERSONS["2"]=${PERSON[@]}
for KEY in "${!PERSONS[@]}"; do
 TMP="${PERSONS["$KEY"]}"
 echo "$KEY - $TMP"
 echo "${TMP["FNAME"]}"
 echo "${TMP["LNAME"]}"
done

The output:

输出:

1 - John Andrew
John Andrew
John Andrew
2 - Elen Murray
Elen Murray
Elen Murray

As you can see trying to access a specific index of the $TMP array in the for loop returns the whole array.

如您所见,在 for 循环中尝试访问 $TMP 数组的特定索引会返回整个数组。

[Q] What do I need to do in order to separately access the "FNAME" and "LNAME" indexes of the $TMP array inside the forloop?

[Q] 为了在for循环内分别访问$TMP数组的“FNAME”和“LNAME”索引,我需要做什么?

Thanks.

谢谢。

回答by glenn Hymanman

You can't do what you're trying to do: bash arraysare one-dimensional

你不能做你想做的事:bash 数组是一维的

$ declare -A PERSONS
$ declare -A PERSON
$ PERSON["FNAME"]='John'
$ PERSON["LNAME"]='Andrew'
$ declare -p PERSON
declare -A PERSON='([FNAME]="John" [LNAME]="Andrew" )'
$ PERSONS[1]=([FNAME]="John" [LNAME]="Andrew" )
bash: PERSONS[1]: cannot assign list to array member

You can fake multidimensionality by composing a suitable array index string:

您可以通过组合合适的数组索引字符串来伪造多维性:

declare -A PERSONS
declare -A PERSON

PERSON["FNAME"]='John'
PERSON["LNAME"]='Andrew'
i=1
for key in "${!PERSON[@]}"; do
  PERSONS[$i,$key]=${PERSON[$key]}
done

PERSON["FNAME"]='Elen'
PERSON["LNAME"]='Murray'
((i++))
for key in "${!PERSON[@]}"; do
  PERSONS[$i,$key]=${PERSON[$key]}
done

declare -p PERSONS
# ==> declare -A PERSONS='([1,LNAME]="Andrew" [2,FNAME]="Elen" [1,FNAME]="John" [2,LNAME]="Murray" )'

回答by sameer Soni

I understand what you need. I also wanted the same for weeks. I was confused whether to use Python or Bash. Finally, exploring something else I found this Bash: How to assign an associative array to another variable name (e.g. rename the variable)?

我明白你需要什么。我也想要同样的数周。我很困惑是使用 Python 还是 Bash。最后,探索其他东西,我发现了这个 Bash:如何将关联数组分配给另一个变量名(例如重命名变量)?

Here, I got to know how to assign some string and use it later as command. Then with my creativity I found solution to your problem as below:-

在这里,我知道如何分配一些字符串并稍后将其用作命令。然后凭借我的创造力,我找到了解决您问题的方法,如下所示:-



#!/bin/bash

declare -A PERSONS
declare -A PERSON

PERSON["FNAME"]='John'
PERSON["LNAME"]='Andrew'
string=$(declare -p PERSON)
#printf "${string}\n"
PERSONS["1"]=${string}
#echo ${PERSONS["1"]}

PERSON["FNAME"]='Elen'
PERSON["LNAME"]='Murray'
string=$(declare -p PERSON)
#printf "${string}\n"
PERSONS["2"]=${string}
#echo ${PERSONS["2"]}

for KEY in "${!PERSONS[@]}"; do
   printf "$KEY - ${PERSONS["$KEY"]}\n"
   eval "${PERSONS["$KEY"]}"
   printf "${PERSONS["$KEY"]}\n"
   for KEY in "${!PERSON[@]}"; do
      printf "INSIDE $KEY - ${PERSON["$KEY"]}\n"
   done
done


OUTPUT:-

输出:-

1 - declare -A PERSON='([FNAME]="John" [LNAME]="Andrew" )'

1 - 声明 -A PERSON='([FNAME]="John" [LNAME]="Andrew" )'

declare -A PERSON='([FNAME]="John" [LNAME]="Andrew" )'

声明 -A PERSON='([FNAME]="John" [LNAME]="Andrew")'

INSIDE FNAME - John

FNAME 内部 - 约翰

INSIDE LNAME - Andrew

内部 LNAME - 安德鲁

2 - declare -A PERSON='([FNAME]="Elen" [LNAME]="Murray" )'

2 - 声明 -A PERSON='([FNAME]="Elen" [LNAME]="Murray" )'

declare -A PERSON='([FNAME]="Elen" [LNAME]="Murray" )'

声明 -A PERSON='([FNAME]="Elen" [LNAME]="Murray")'

INSIDE FNAME - Elen

内幕 FNAME - 艾伦

INSIDE LNAME - Murray

INSIDE LNAME - 默里



The problem actually with multi dimensional arrays in bash and specifically in your approach is that you are assigning PERSON array values to the array element PERSONS[1] which is converted to a list and not an assoc array when you assigned it. And so it no longer will take it as 2 elements of an array as you are not keeping any info about the array data structure in your value. So, I found this hack to be sufficient with only 1 limitation that you will have to do this each time you want to do store/retrieve values. But it shall solve your purpose.

bash 中的多维数组实际上存在的问题,特别是在您的方法中,您将 PERSON 数组值分配给数组元素 PERSONS[1] ,当您分配它时,它被转换为列表而不是关联数组。因此它不再将它作为数组的 2 个元素,因为您没有在您的值中保留有关数组数据结构的任何信息。所以,我发现这个 hack 就足够了,只有 1 个限制,你每次想要存储/检索值时都必须这样做。但它会解决你的目的。

回答by Raymondo

#!/bin/bash
declare -A PERSONS
declare -A PERSON
PERSON["FNAME"]='John'
PERSON["LNAME"]='Andrew'
PERSONS["1"]=${PERSON[@]}
PERSON["FNAME"]='Elen'
PERSON["LNAME"]='Murray'
PERSONS["2"]=${PERSON[@]}
for KEY in "${!PERSONS[@]}"; do
 TMP="${PERSONS["$KEY"]}"
 echo "$KEY - $TMP"
 **echo "${PERSON["FNAME"]}"
 echo "${PERSON["LNAME"]}**"
done

Because the original array for the separate names is still active and it can still be referenced within the forloop.

因为单独名称的原始数组仍然处于活动状态,并且仍然可以在for循环中引用。