Multidimensional associative arrays / hashes in KSH
Recently I discovered how to use multidimensional hashes in Korn Shell. Here are some short code sniplets which show how they work.
Create a multidimensional hash variable:1
2
3
4
5
6
7
8
9
10
11$ typeset -A a=([b]='')
$ typeset -A a[b]=([c]='')
$ typeset -A a[b][c]=([d]='e')
$ printf "%B\n" a
(
[b]=(
[c]=(
[d]=e
)
)
)
Extend it with additional values:1
2
3
4
5
6
7
8
9
10$ typeset -A a[b][c]+=([f]='g')
$ printf "%B\n" a
(
[b]=(
[c]=(
[d]=e
[f]=g
)
)
)
Work with the dimensions to manipulate output:1
2
3
4
5
6
7
8
9
10$ echo ${!a[*]}
b
$ echo ${!a[b][*]}
c
$ echo ${!a[b][c][*]}
d f
$ echo ${a[b][c][d]}
e
$ echo ${a[b][c][f]}
g
Hope it helps,
visit