Linux 中 关联数组的应用

 

001、关联数组的声明

declare -A array1

 

002、关联数组的赋值

[root@localhost test]# declare -A array1
[root@localhost test]# array1["a"]=1000
[root@localhost test]# array1["x"]=5555
[root@localhost test]# array1["d"]="kkkk"
[root@localhost test]# echo ${array1[*]}
5555 kkkk 1000

 。

 

003、输出关联数组的长度

[root@localhost test]# echo ${array1[*]}
5555 kkkk 1000
[root@localhost test]# echo ${#array1[*]}
3
[root@localhost test]# echo ${#array1[@]}       ## 输出关联数组的长度
3

 。

 

004、输出关联数组的索引值

[root@localhost test]# echo ${array1[*]}
5555 kkkk 1000
[root@localhost test]# echo ${!array1[*]}
x d a

 。

 

005、 关联数组的遍历

[root@localhost test]# echo ${array1[*]}
5555 kkkk 1000
[root@localhost test]# for i in ${array1[*]}; do echo $i; done
5555
kkkk
1000
[root@localhost test]# for i in ${array1[@]}; do echo $i; done
5555
kkkk
1000
[root@localhost test]# for i in "${array1[*]}"; do echo $i; done
5555 kkkk 1000
[root@localhost test]# for i in "${array1[@]}"; do echo $i; done
5555
kkkk
1000

 。

 

posted @ 2025-02-23 15:58  小鲨鱼2018  阅读(12)  评论(0)    收藏  举报