Saturday, March 24, 2018

Handling Array(s) with Windows Power Shell

It is the same as a .NET array. In .NET System.Array is based on System.Object. Working with arrays in Power Shell is somewhat easier than in C#.

Assigning as Array. $ stands for a variable.


You assign the array to a variable 'flowers' as shown above. When you call the variable the array will be printed in the console as shown above.

This is same written explicitly as follows:

$flowers=@("rose", "jasmine", "lily") 
Note array elements need not be of the same type.

Writing array content to console
You can also print to the console as shown in the next. $_ is a dynamic variable storing current information.


Write-Host $_:    write current information of each element you find to the console
|                      :   Pipe command. Get the command output and pass it to another

Finding its Type
The next code shows the type of 'flowers' as an Object.


Finding an array element by its index


Add another array item to the array. 'Sun Flower' is added. Adding element using the += operator is slow, using the System.Collections.ArrayList as a New Object is faster for additions. 


Find the last elements(array items) of the array. It is zero based. 
[ .. ] is called a range operator.


Reversing an array (last becomes first and first becomes last). :: stands for static member of a class


Start with an empty array and add elements to it. @ is array operator and @() is an empty array.


Searching with wild cards in an array (in names above)

Note:
*a finds two items
*ar finds only one

Create a 2D array.

Find the second element of the second array in the above


Add a new item to the second array




No comments:

DMCA.com Protection Status