Blog Post

#PowershellBasics: Working with arrays

,

I mentioned arrays in a previous post so I figured I should talk about what they are and how to work with them. Now, I’m going to assume that as a DBA you may not really know what an array is (although you probably do) so here is a quick definition:

An array is a data structure that is designed to store a collection of items. The items can be the same type or different types.

Source

An array can have one or more dimensions (a column, a box, a cube etc) and can have a fixed data type or a not. If you don’t have a fixed data type then each element can any type of data. A integer, a string, a horse, etc. Well, maybe not a horse but it can contain any type of coding object. I tend to think of the fixed data type arrays as tables. A single dimension is a single column table, a two dimension table is multi column table, etc. Arrays without a fixed data type are more like excel spreadsheets.

Working with arrays is pretty easy. You declare them with []s or by just assigning a list of elements to a variable. If you are declaring them using []s then one pair will make a single dimension array, two pairs will make a two dimension array etc.

Here are a couple of examples:

[string[]]$TestArray = "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"
#Or
$TestArray = "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"

[string[][]]$TestArray2 = ("Zero,Zero","Zero,One","Zero,Two","Zero,Three")`
     ,("One,Zero","One,One","One,Two","One,Three")`
     ,("Two,Zero","Two,One","Two,Two","Two,Three")`
     ,("Three,Zero","Three,One","Three,Two","Three,Three")
#Or
$TestArray2 = ("Zero,Zero","Zero,One","Zero,Two","Zero,Three")`
     ,("One,Zero","One,One","One,Two","One,Three")`
     ,("Two,Zero","Two,One","Two,Two","Two,Three")`
     ,("Three,Zero","Three,One","Three,Two","Three,Three")

At this point I should probably point out that arrays are 0 based. By that I mean the first element of an array is the 0 element not the 1 element. A bit confusing if you ask me but you’ll get used to it. Now, to reference an element you just have to put the position number in the []s. The strings I put in the arrays are there so that it’s really obvious that I’m getting back the right values.

$TestArray[2]
Two
$TestArray[7]
Seven
$TestArray2[2][1]
Two, One
$TestArray2[0][3]
Zero, Three

Now here’s where it gets interesting. This is Powershell, and one of the things that makes Powershell so .. well .. Powerfull .. is that you can pass multiple elements to a command and it will process it for each of them. So to do that we need to be able to get multiple elements back.

All elements

$TestArray2
Zero,Zero
Zero,One
Zero,Two
Zero,Three
One,Zero
One,One
One,Two
One,Three
Two,Zero
Two,One
Two,Two
Two,Three
Three,Zero
Three,One
Three,Two
Three,Three

Multiple elements in a row

$TestArray[3..7]
Three
Four
Five
Six
Seven

Multiple elements that aren’t in a row

$TestArray2[0,2][1]
Zero, One
Two, One
$TestArray2[0][1,3]
Zero, One
Zero, Three

Where this got really weird is that I couldn’t figure out how to pull multiple elements from both parts of the array at once. For example this didn’t return what I expected.

$TestArray2[0,2][1,3]
Two, Zero
Two, One
Two, Two
Two, Three

Obviously I’m still pretty new at this so if I’m missing something or I’m flat out wrong somewhere feel free to make a note in the comments below and I’ll update the post ??

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

2 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

2 (1)

You rated this post out of 5. Change rating