| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # NOTE: |
| 4 | # -declare -A is required. |
| 5 | # |
| 6 | # Simply doing: |
| 7 | # a=([aa]=b [foo]=bar ['a+1']=c) |
| 8 | # gets utterly bizarre behavior. |
| 9 | # |
| 10 | # Associtative Arrays are COMPLETELY bash-specific. mksh doesn't even come |
| 11 | # close. So I will probably not implement them, or implement something |
| 12 | # slightly different, because the semantics are just wierd. |
| 13 | |
| 14 | # http://www.gnu.org/software/bash/manual/html_node/Arrays.html |
| 15 | # TODO: Need a SETUP section. |
| 16 | |
| 17 | #### TODO: SETUP should be share |
| 18 | declare -A a |
| 19 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 20 | |
| 21 | #### create empty assoc array, put, then get |
| 22 | declare -A d # still undefined |
| 23 | d['foo']=bar |
| 24 | echo ${d['foo']} |
| 25 | ## stdout: bar |
| 26 | |
| 27 | #### retrieve indices with ! |
| 28 | declare -A a |
| 29 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 30 | argv.py "${!a[@]}" |
| 31 | # Is this invalid on associative arrays? Makes no sense. |
| 32 | ## stdout: ['foo', 'aa', 'a+1'] |
| 33 | |
| 34 | #### $a gives nothing |
| 35 | declare -A a |
| 36 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 37 | echo "${a}" |
| 38 | ## stdout-json: "\n" |
| 39 | |
| 40 | #### length of dict does not work |
| 41 | declare -A a |
| 42 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 43 | echo "${#a}" |
| 44 | ## stdout: 0 |
| 45 | |
| 46 | #### index by number doesn't work |
| 47 | declare -A a |
| 48 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 49 | echo 0 "${a[0]}" 1 "${a[1]}" 2 "${a[2]}" |
| 50 | ## stdout-json: "0 1 2 \n" |
| 51 | |
| 52 | #### index by key name |
| 53 | declare -A a |
| 54 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 55 | echo "${a[aa]}" "${a[foo]}" "${a['a+1']}" |
| 56 | # WTF: Why do we get bar bar c? |
| 57 | ## stdout-json: "b bar c\n" |
| 58 | |
| 59 | #### index by quoted string |
| 60 | declare -A a |
| 61 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 62 | echo "${a['a+1']}" |
| 63 | ## stdout: c |
| 64 | |
| 65 | #### index by unquoted string |
| 66 | declare -A a |
| 67 | a=([aa]=b [foo]=bar ['a+1']=c) |
| 68 | echo "${a[a+1]}" |
| 69 | ## stdout: c |
| 70 | |
| 71 | #### index by unquoted string as arithmetic |
| 72 | # For assoc arrays, unquoted string is just raw. |
| 73 | # For regular arrays, unquoted string is an arithmetic expression! |
| 74 | # How do I parse this? |
| 75 | declare -A assoc |
| 76 | assoc=([a+1]=c) |
| 77 | array=(5 6 7) |
| 78 | a=1 |
| 79 | echo "${assoc[a]}" |
| 80 | echo "${assoc[a+1]}" # This works |
| 81 | echo "${array[a+1]}" |
| 82 | ## stdout-json: "\nc\n7\n" |
| 83 | |
| 84 | #### WTF index by key name |
| 85 | declare -A a |
| 86 | a=([xx]=bb [cc]=dd) |
| 87 | echo "${a[xx]}" "${a[cc]}" |
| 88 | ## stdout-json: "bb dd\n" |
| 89 | |
| 90 | #### Array stored in associative array gets converted to string |
| 91 | array=('1 2' 3) |
| 92 | declare -A d |
| 93 | d[a]="${array[@]}" |
| 94 | argv.py "${d[a]}" |
| 95 | ## stdout: ['1 2 3'] |
| 96 | |
| 97 | #### Can't initialize assoc array with indexed array |
| 98 | declare -A A=(1 2 3) |
| 99 | ## status: 1 |
| 100 | ## BUG bash status: 0 |
| 101 | |
| 102 | #### Initializing indexed array with with assoc array drops the constants |
| 103 | declare -a a=([xx]=1 [yy]=2 [zz]=3) |
| 104 | #declare -a a=(1 2 3) |
| 105 | echo "${a[@]}" |
| 106 | #echo "${!a[@]}" |
| 107 | ## N-I mksh stdout-json: "" |
| 108 | ## BUG bash stdout-json: "3\n" |
| 109 | |
| 110 | #### Append to associative array value |
| 111 | declare -A a |
| 112 | a['x']+='foo' |
| 113 | a['x']+='bar' |
| 114 | argv.py "${a["x"]}" |
| 115 | ## STDOUT: |
| 116 | ['foobar'] |
| 117 | ## END |
| 118 |