Monday, April 2, 2018

Multidimensional arrays in PowerShell

There isn't convenient syntax in PowerShell for creating a multidimensional array, but the .NET style works just fine. For example, this creates a 5x3 two-dimensional array of integers:

$arr = [array]::CreateInstance([int], 5, 3)

The syntax for using these arrays is exactly what you would expect. This sets the element at both dimensions' first index:

$arr[0, 0] = 1

Interestingly, ranges (or collections in general) are not acceptable indexes for multidimensional arrays like they are for one-dimensional arrays.

No comments:

Post a Comment