Friday, September 06, 2013

PowerShell Array’s inside-out

 
Learning PowerShell is one thing, but learning arrays in PowerShell is another. Since i started using PowerShell as my primary programming- and scripting language i have stumbled on this array stone many times.

Now it is time to really get to the bottom of this.

What’s an array

Arrays are, bluntly put, programming variables with more functionality and more space in this view an array can be used as a variable in your PowerShell code. as a matter of fact most beginners do experience arrays as a variable certainly in the beginning. that is because arrays do tend to work exactly like  a variable when it only has one item and one value. look at this example:
[string]$Variable="one value"
[array]$array= @("one value")

$Variable
"---------"
$array

This code will display exactly the same return codes when run.

one value
---------
one value

now look at this:
[string]$Variable="one value","one value","one value"
[array]$array= @("one value","one value","one value")

$Variable
"---------"
$array
this will output:
one value one value one value
---------
one value
one value
one value

Here you will see the first differences between a variable and an array. The variable will handle the value as one string while the array will display 3 strings in an row.

PowerShell will use default values for options you do not present. In this case PowerShell will present the entire $variable as a string because you defined the variable as a string so the output will also be a string. with the array we defined a one dimensional array. because of that the output of the array to the default output (screen) will the the presentation of 3 objects, each presented on its own row. you can see the differences even more when you request PowerShell to return a specific item of a variable or array:
[string]$Variable="one value","one value","one value"
[array]$array= @("one value","one value","one value")


PS C:\Windows\system32> $Variable[0]
o

PS C:\Windows\system32> $array[0]
one value

As can be seen from this example the first item of the variable is the first character of the string while the first item of the array is the first object.

Array dimensions


Arrays come in two flavours: one-dimensional and multi-dimensional. the one-dimensional is shown above so lets look at multi-dimensional arrays:

a multi-dimensional array is defined like this:
$MultiArray = @(("one","two"),
             ("three","four","six"))



To retrieve the entire array a simple $MultiArray will suffice but retrieving a specific item from an array like this is done a bit different.

Say you would like to retrieve the second item from the second row, how do you do that?
$MultiArray[1][1]
four

So the trick is to define the item you want to retrieve and define it in square brackets [ ] one for the row to select (the number in the first bracket) and one number for the column (the number in the second bracket) the count of rows and columns is from 0, so row 1 is [0]

Handy tricks in multi-dimensional arrays


There are some nice ‘tricks’ with multi-dimensional arras:


  • retrieve the last item of a row:
$MultiArray = @(("one","two"),
             ("three","four","six"))

$MultiArray[1][-1]
six

So you give a –1 for the last item to be retrieved. so how to retrieve the second last item of row 1?
$MultiArray = @(("one","two"),
             ("three","four","six"))

$MultiArray[0][-1-1]
one


  • retrieve items 2 to 4 from row 2 from an array
$MultiArray = @(("one","two"),
             ("three","four","six","seven","eight","nine"))

$MultiArray[1][2..4]
six
seven
eight

To really get this down have some fun with it like building a loop to get things done:
$MultiArray = @(("one","two"),
             ("three","four","six","seven","eight","nine"),
             ("ten","eleven")
)

$noRows=$MultiArray.Count

for($i=0;$i - $noRows;$i++){$rownow=$null
    [string]$rownow += $MultiArray[$i]
    [array]$gridarray += $rownow}
$gridarray

one two
three four six seven eight nine
ten eleven

That is about it, you should really have a go and do some things with arrays to get to know them well.

See you next time.

Tags van Technorati: ,,

No comments:

Post a Comment