1 #
2 # Differences from bash:
3 # - literal syntax alternates key-value
4 # - (@k) syntax for keys. Although this is sort of like my ${@array} syntax
5 # for arrays.
6 # - zsh allows $a[$k], not just ${a[$k]}
7
8
9 #### TODO: SETUP should be shared
10 typeset -A a
11 a=(aa b foo bar a+1 c)
12
13 #### retrieve key
14 typeset -A a
15 a=(aa b foo bar a+1 c)
16 echo ${a[aa]}
17 ## stdout: b
18
19 #### set key
20 typeset -A a
21 a=(aa b foo bar a+1 c)
22 a[X]=XX
23 argv.py "${a[@]}"
24 # What order is this?
25 ## stdout: ['bar', 'b', 'c', 'XX']
26
27 #### iterate over keys
28 typeset -A assoc
29 assoc=(k1 v1 k2 v2 k3 v3)
30 for k in "${(@k)assoc}"; do
31 echo "$k: $assoc[$k]"
32 done
33 ## stdout-json: "k1: v1\nk2: v2\nk3: v3\n"
34
35 #### iterate over both keys and values
36 typeset -A assoc
37 assoc=(k1 v1 k2 v2 k3 v3)
38 for k v ("${(@kv)assoc}"); do
39 echo "$k: $v"
40 done
41 ## stdout-json: "k1: v1\nk2: v2\nk3: v3\n"
42
43 #### get length
44 typeset -A assoc
45 assoc=(k1 v1 k2 v2 k3 v3)
46 echo ${#assoc} ${#assoc[k1]}
47 ## stdout: 3 2
48
49 #### index by integer does not work
50 typeset -A assoc
51 assoc=(k1 v1 k2 v2 k3 v3)
52 argv.py "${assoc[1]}"
53 ## stdout: ['']