|
| 1 | +--- |
| 2 | +Title: 'Arrays' |
| 3 | +Description: 'Arrays are a data structure that holds a collection of items. PowerShell provides many ways to access, update, and manipulate array items.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | + - 'Bash/Shell' |
| 8 | +Tags: |
| 9 | + - 'Arrays' |
| 10 | + - 'Data Types' |
| 11 | + - 'Data Structures' |
| 12 | + - 'Index' |
| 13 | + - 'Lists' |
| 14 | + - 'Operators' |
| 15 | + - 'Variable Types' |
| 16 | +CatalogContent: |
| 17 | + - 'learn-powershell' |
| 18 | + - 'paths/computer-science' |
| 19 | +--- |
| 20 | + |
| 21 | +**Arrays** are a [data structure](https://www.codecademy.com/resources/docs/general/data-structures) that holds a collection of items. Items can be of the same type or multiple types. |
| 22 | + |
| 23 | +## Creating an Array |
| 24 | + |
| 25 | +Separating items by commas (`,`) is the simplest way to create an array in PowerShell. |
| 26 | + |
| 27 | +```shell |
| 28 | +$my_arr = 25, "Codecademy", 1, $False |
| 29 | +``` |
| 30 | + |
| 31 | +Alternatively, the array subexpression operator `@( )` can be used. Anything placed within the parentheses is treated as an item of the array. |
| 32 | + |
| 33 | +```shell |
| 34 | +$arr_1 = @($True, 5, (Get-Date).DateTime) # 3 elements |
| 35 | +$arr_2 = @( ) # Empty Array |
| 36 | +$arr_3 = @( # Multi-line Array |
| 37 | + "Uno" |
| 38 | + "Dos" |
| 39 | + "Tres" |
| 40 | +) |
| 41 | +``` |
| 42 | + |
| 43 | +## Accessing Array Items |
| 44 | + |
| 45 | +The items in an array are accessed using their index, or their position in the array. Consider the array: |
| 46 | + |
| 47 | +```shell |
| 48 | +$colors = "red", "yellow", "black", "blue" |
| 49 | +``` |
| 50 | + |
| 51 | +The indexes in PowerShell start at `0`. |
| 52 | + |
| 53 | +| Index | Value | |
| 54 | +| :---: | :--------: | |
| 55 | +| `0` | `"red"` | |
| 56 | +| `1` | `"yellow"` | |
| 57 | +| `2` | `"black"` | |
| 58 | +| `3` | `"blue"` | |
| 59 | + |
| 60 | +Brackets `[ ]` are used to access an item in an array. To access `"black"` in the `$colors` array, for example: |
| 61 | + |
| 62 | +```shell |
| 63 | +PS > $colors[2] |
| 64 | +black |
| 65 | +``` |
| 66 | + |
| 67 | +## Updating Array Items |
| 68 | + |
| 69 | +Items can be updated — or changed – by utilizing indexes. To change the color `yellow` to `brown`, for example: |
| 70 | + |
| 71 | +```shell |
| 72 | +PS > $colors[1] = "brown" |
| 73 | +PS > $colors |
| 74 | +red |
| 75 | +brown |
| 76 | +black |
| 77 | +blue |
| 78 | +``` |
| 79 | + |
| 80 | +## Indexing |
| 81 | + |
| 82 | +PowerShell offers flexibility when indexing items, such as: |
| 83 | + |
| 84 | +- Multiple indexes: Separate indexes with commas to print multiple items. |
| 85 | + |
| 86 | +```shell |
| 87 | +PS > $colors[0,2] |
| 88 | +red |
| 89 | +black |
| 90 | +``` |
| 91 | + |
| 92 | +- Range operator `..`: Print all items between two indexes (exclusive). |
| 93 | + |
| 94 | +> **Note:** This syntax is exclusive since the stop index `3` is not included. |
| 95 | +
|
| 96 | +```shell |
| 97 | +PS > $colors[1..3] |
| 98 | +brown |
| 99 | +black |
| 100 | +blue |
| 101 | +``` |
| 102 | + |
| 103 | +- Reverse range: Use range operator from a higher index to a lower index to print items in reverse order (inclusive). |
| 104 | + |
| 105 | +> **Note:** This syntax is inclusive since both the start and stop indexes, `2` and `1`, are included. |
| 106 | +
|
| 107 | +```shell |
| 108 | +PS > $colors[2..1] |
| 109 | +black |
| 110 | +brown |
| 111 | +``` |
| 112 | + |
| 113 | +- Negative indexes: Items are referenced in reverse order where the last item has an index of `-1`. |
| 114 | + |
| 115 | +```shell |
| 116 | +PS > $colors[-1] |
| 117 | +blue |
| 118 | +``` |
| 119 | + |
| 120 | +## Iteration |
| 121 | + |
| 122 | +Each array object has a method called `ForEach` which can be utilized to perform the same action on each of its items. PowerShell defines the variable `$PSItem` or just underscore `_` to refer to each item in the array. |
| 123 | + |
| 124 | +```shell |
| 125 | +PS > $colors.ForEach({ $PSItem.Length }) # $_.Length also works |
| 126 | +3 |
| 127 | +5 |
| 128 | +5 |
| 129 | +4 |
| 130 | +``` |
| 131 | + |
| 132 | +The example above is printing the length of each string of our `colors` array. `red` has a length of `3`, `brown` is `5`, and so on. |
| 133 | + |
| 134 | +## Array Operators |
| 135 | + |
| 136 | +### Addition Operator |
| 137 | + |
| 138 | +The addition operator `+` concatenates – or combines – two arrays. |
| 139 | + |
| 140 | +```shell |
| 141 | +PS > $fibonacci_1 = 0, 1, 1 |
| 142 | +PS > $fibonacci_2 = 2, 3, 5 |
| 143 | +PS > $fibonacci_1 + $fibonacci_2 |
| 144 | +0 |
| 145 | +1 |
| 146 | +1 |
| 147 | +2 |
| 148 | +3 |
| 149 | +5 |
| 150 | +``` |
| 151 | + |
| 152 | +### Multiplication Operator |
| 153 | + |
| 154 | +The multiplication operator `*` copies the array a specified number of times. |
| 155 | + |
| 156 | +```shell |
| 157 | +PS > $fibonacci_2 * 2 |
| 158 | +2 |
| 159 | +3 |
| 160 | +5 |
| 161 | +2 |
| 162 | +3 |
| 163 | +5 |
| 164 | +``` |
| 165 | + |
| 166 | +### Containment Operators |
| 167 | + |
| 168 | +Containment operators check whether or not an item is in an array and returns a boolean. |
| 169 | + |
| 170 | +| Operator | Syntax | Example | |
| 171 | +| :------------: | :---------------------------: | :-----------------------------------------: | |
| 172 | +| `-contains` | `<array> -contains <item>` | `$fibonacci -contains 4` returns `False`. | |
| 173 | +| `-notcontains` | `<array> -notcontains <item>` | `$fibonacci -notcontains 4` returns `True`. | |
| 174 | +| `-in` | `<item> -in <array>` | `5 -in $fibonacci` returns `True`. | |
| 175 | +| `-notin` | `<item> -notin <array>` | `5 -notin $fibonacci` returns `False`. | |
| 176 | + |
| 177 | +### `-join` |
| 178 | + |
| 179 | +The `-join` operator combines the items in an array into a string separated by a character or a string. Consider the following example: |
| 180 | + |
| 181 | +```shell |
| 182 | +PS > $fibonacci = 0, 1, 1, 2, 3, 5 |
| 183 | +PS > $fibonacci = $fibonacci -join "->" |
| 184 | +PS > $fibonacci |
| 185 | +0->1->1->2->3->5 |
| 186 | +PS > $fibonacci.GetType().Name |
| 187 | +String |
| 188 | +``` |
| 189 | + |
| 190 | +## Strongly Typed Arrays |
| 191 | + |
| 192 | +Types can be casted onto arrays to force each item in the array to adhere to that type. |
| 193 | + |
| 194 | +```shell |
| 195 | +PS > [String[]]$fruits = "apple", "banana", "kiwi" |
| 196 | +``` |
| 197 | +
|
| 198 | +## Objects Arrays |
| 199 | +
|
| 200 | +Arrays can hold objects. |
| 201 | +
|
| 202 | +```shell |
| 203 | +$dogs_arr = @( |
| 204 | + [PSCustomObject]@{Name = 'Rufus'; Age = 10} |
| 205 | + [PSCustomObject]@{Name = 'Miku'; Age = 2} |
| 206 | +) |
| 207 | +``` |
| 208 | +
|
| 209 | +Each object in the array as well as its properties and methods can be accessed individually. |
| 210 | +
|
| 211 | +```shell |
| 212 | +PS > $dogs_arr.ForEach({ $PSItem.Name + " is " + $PSItem.Age + " years old."}) |
| 213 | +Rufus is 10 years old. |
| 214 | +Miku is 2 years old. |
| 215 | +``` |
| 216 | +
|
| 217 | +> **Note:** `$PSItem` can be replaced with its shorthand alias `$_`. |
0 commit comments