| 1 | ---
 | 
| 2 | in_progress: yes
 | 
| 3 | css_files: ../../web/base.css ../../web/manual.css ../../web/toc.css
 | 
| 4 | ---
 | 
| 5 | 
 | 
| 6 | Draft
 | 
| 7 | =====
 | 
| 8 | 
 | 
| 9 | ## Operations on Arrays
 | 
| 10 | 
 | 
| 11 | ### Initialization
 | 
| 12 | 
 | 
| 13 |     declare -a array 
 | 
| 14 |     declare -a array=()
 | 
| 15 | 
 | 
| 16 |     declare -A assoc
 | 
| 17 |     # there is no empty literal here
 | 
| 18 | 
 | 
| 19 | Also valid, but not necessary since `declare` is local:
 | 
| 20 | 
 | 
| 21 |     local -a array
 | 
| 22 |     local -A assoc
 | 
| 23 | 
 | 
| 24 | Makes a global array:
 | 
| 25 | 
 | 
| 26 |     array=()
 | 
| 27 | 
 | 
| 28 | ### Array Literals
 | 
| 29 | 
 | 
| 30 | Respects the normal rules of argv.
 | 
| 31 | 
 | 
| 32 |     prefix=foo
 | 
| 33 |     myarray=(one two -{three,four}- {5..8} *.py "$prefix*.py" '$prefix*.py')
 | 
| 34 | 
 | 
| 35 |     myarray=(
 | 
| 36 |       $var ${var} "$var" 
 | 
| 37 |       $(echo hi) "$(echo hi)"
 | 
| 38 |       $(1 + 2 * 3)
 | 
| 39 |     )
 | 
| 40 | 
 | 
| 41 | ### Associative Array Literals
 | 
| 42 | 
 | 
| 43 |     (['k']=v)
 | 
| 44 | 
 | 
| 45 |     Unlike bash, ([0]=v) is still an associative array literal.
 | 
| 46 | 
 | 
| 47 |     It's not an indexed array literal.  This matters when you take slices and
 | 
| 48 |     so forth?
 | 
| 49 | 
 | 
| 50 | 
 | 
| 51 | ### "${a[@]}" is Evaluating (Splicing)
 | 
| 52 | 
 | 
| 53 |     echo "${array[@]}"
 | 
| 54 |     echo "${assoc[@]}"
 | 
| 55 | 
 | 
| 56 | Not Allowed, unlike in bash!
 | 
| 57 | 
 | 
| 58 |     $assoc  ${assoc}  "${assoc}"
 | 
| 59 |     ${!assoc}  ${assoc//pattern/replace}  # etc.
 | 
| 60 | 
 | 
| 61 | 
 | 
| 62 | ### Iteration
 | 
| 63 | 
 | 
| 64 | Note that since a for loop takes an array of words, evaluating/splicing works:
 | 
| 65 | 
 | 
| 66 |     for i in "${a1[@]}" "${a2[@]}"; do
 | 
| 67 |       echo $i
 | 
| 68 |     done
 | 
| 69 | 
 | 
| 70 | ### ${#a[@]} is the Length
 | 
| 71 | 
 | 
| 72 | 
 | 
| 73 |     echo ${#array[@]}
 | 
| 74 |     echo ${#assoc[@]}
 | 
| 75 | 
 | 
| 76 | 
 | 
| 77 | ### Coercion to String by Joining Elements
 | 
| 78 | 
 | 
| 79 |     echo ${!array[@]}
 | 
| 80 |     echo ${!assoc[@]}
 | 
| 81 | 
 | 
| 82 |     echo ${!array[*]}
 | 
| 83 |     echo ${!assoc[*]}
 | 
| 84 | 
 | 
| 85 |     echo "${!array[*]}"
 | 
| 86 |     echo "${!assoc[*]}"
 | 
| 87 | 
 | 
| 88 | ### Look Up By Index / Key With a[]
 | 
| 89 | 
 | 
| 90 |   matrix:
 | 
| 91 |     a['x'] a["x"]
 | 
| 92 |     a["$x"]
 | 
| 93 |     a[$x]
 | 
| 94 |     a[${x}]
 | 
| 95 |     a[${x#a}]
 | 
| 96 | 
 | 
| 97 |     a[x] -- allowed
 | 
| 98 |     A[x] -- NOT allowed?  It should be a string
 | 
| 99 | 
 | 
| 100 |     (( 'a' )) -- parsed, but can't evaluate
 | 
| 101 | 
 | 
| 102 |     # This is a string in both cases
 | 
| 103 |     a[0]
 | 
| 104 |     A[0]
 | 
| 105 | 
 | 
| 106 | 
 | 
| 107 | undef[0]=1 automatically creates an INDEXED array
 | 
| 108 | undef=(1)
 | 
| 109 | 
 | 
| 110 | ### Assign / Append To Location Specified by Index / Key
 | 
| 111 | 
 | 
| 112 |     a[expr]=    # int_coerce
 | 
| 113 |     A[expr]=    # no integer coercion
 | 
| 114 | 
 | 
| 115 | Just like you can append to strings:
 | 
| 116 | 
 | 
| 117 |     s+='foo'
 | 
| 118 | 
 | 
| 119 | Append to elements of an array, which are strings:
 | 
| 120 | 
 | 
| 121 |     a[x+1]+=x
 | 
| 122 |     a[x+1]+=$x
 | 
| 123 | 
 | 
| 124 | ### Slicing With ${a[@]:5:2}
 | 
| 125 | 
 | 
| 126 |     ${array[@]:1:3}
 | 
| 127 | 
 | 
| 128 | Note the presence of DISALLOWED VALUES.
 | 
| 129 | 
 | 
| 130 | 
 | 
| 131 |     # TODO: disallow this?  because no order
 | 
| 132 |     ${assoc[@]:1:3}
 | 
| 133 | 
 | 
| 134 | 
 | 
| 135 | NOTE: string slicing:
 | 
| 136 | 
 | 
| 137 | 
 | 
| 138 | 
 | 
| 139 | ### Append Array to Array
 | 
| 140 | 
 | 
| 141 |     a=(1 2 3)
 | 
| 142 |     a+=(4 5 6)
 | 
| 143 | 
 | 
| 144 | 
 | 
| 145 | ### Get All Indices With ${!a[@]}
 | 
| 146 | 
 | 
| 147 |     echo ${!array[@]}
 | 
| 148 |     echo ${!assoc[@]}
 | 
| 149 | 
 | 
| 150 | 
 | 
| 151 | ### Vectorized String Operations
 | 
| 152 | 
 | 
| 153 |     echo ${array[@]//x/X}
 | 
| 154 | 
 | 
| 155 |     echo ${assoc[@]//x/X}
 | 
| 156 | 
 |