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