Thursday, October 16, 2014

#29 : Creating array in Powershell

Array is programming construct of most of the high level languages. It is meant to describe a collection of elements (values or variables), each selected by one or more indices that can be computed at run time by the program.

Declaring array is very simple in Powershell and there are multiple ways to achieve it :

1. Static Array

Array : Definition  
An array type is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices that can be computed at run time by the program. Such a collection is usually called an array.
Array can be useful in your programming where you have to work on a collection of

Creating an Array in Powershell:

  1. $x=@()   
  2. $x += "Som"   
  3. $x += "Jack"   
  4. $x += "John"   
  5. $x += "Micky"   

Displaying and accessing elements of Array:
You can access each element of array with [<index_number>]. Square brackets enclosed in index number.
  1. $x[0]  
  2. $x 
Output:
Som
Som
jack
John
Mickey 


Searching an element of Array:
Array plays important role in searching in Powershell. Let's see how?

Suppose we have to search if jack exists in any of index of the array, we can search it using below statement -
    1. $x -contains "jack"    
    2. $x -contains "xx"   
    Output:
    True
    False 
     So, you may use it in your scripts like this -
    1. if ( $x -contains "jack" )   
    2. {   
    3.     Write-Host "jack exits!"   
    4. }   
    5. else   
    6. {   
    7.     Write-Host "jack does not exits!!"   
    8. }   


    Deleting an Element from Array:

    There is no direct method which can be used to delete specific index from array. But below can be tried to search and delete specific value from an Array -
    1. clear   
    2. $x=@()   
    3. $x += "Som"   
    4. $x += "Som"   
    5. $x += "Som"   
    6. $x += "Ravi"   
    7. $x += "jack"   
    8.   
    9. Write-Host "Before Deleting element named 'jack' - "    
    10. $x   
    11.   
    12. $x = $x | where {$_ -ne "jack" }   
    13.   
    14. Write-Host "After Deleting element named 'jack' - "    
    15. $x  



    Array is one of the basics of any Programming language. There are numerous other ways to create Array in Powershell.



    cls




    #--Static Array --#
     

     

    $x1 = (1,2,3)




    $x1
     
     

     
    #-OR- Write like below --#
     
     

    $x = 1 ,2 ,3


     
     
    echo "#--Just writing $x shows all elements --# "



    $x
     
     

    echo "#--Onliner to print all elements of array --# "
    $x | foreach { echo "Element - $($_)"}



     
     
    echo "#--Display all elements using foreach --# "
    foreach ( $i in $x )



    {
     
    echo "Element - $i"



    }
     
    echo "#--Display all elements using for loop --# "

    for ($c=0; $c -lt $x.Count; $c++)



    {
     
    echo "Element[$c]- $($x[$c])"



    }
     
    $y = 1 , "Som" , "Testing" , 3



    $y
     
     



     
     
    echo "#--Add two arrays --# "
    $z = $x + $y

    $z



     
     
    echo "#--Substract elements of one array --# "




    #--Below does $y -$x --#
     
     
    $n= $y | where { $x -notcontains $_ }
    $n



     
     
    echo "#--Range --#"

    $m = 1 ..6
    $m



     
     
    echo "--Adding elements to array--#"

    $Arr = @()
    $Arr += "Check-1"
    $Arr += "Check-2"
    $Arr += "Check-3"
    $Arr += "Check-4"

    $Arr



     
     
    echo "--Create Multi-dimensional Array 4x4--#"

    [string[][]]$md = @( (0,0,0,0) , (0,0,0,0), (0,0,0,0), (0,0,0,0) )

    $md[0][0]="A"
    $md[0][1]="B"
    $md[0][2]="C"
    $md[0][3]="D"

    $md[0]

    echo "------------"

    $md[1][0]="E"
    $md[1][1]="F"
    $md[1][2]="G"
    $md[1][3]="H"

    $md[1]

    echo "------------"

    $md[2][0]="I"
    $md[2][1]="J"
    $md[2][2]="K"
    $md[2][3]="L"

    $md[2]

    echo "------------"

    $md[3][0]="M"
    $md[3][1]="N"
    $md[3][2]="O"
    $md[3][3]="P"

    $md[3]




    #--Formatted display of 4x4 multi-dimensional Array--#
     
     
    echo " "
    echo "+---+---+---+---+---+"
    echo "| | 0 | 1 | 2 | 3 |"
    echo "+---+---+---+---+---+"

    for ( $a=0; $a -lt 4 ; $a++)



    {
     
    echo "| $a | $($md[$a][0]) | $($md[$a][1]) | $($md[$a][2]) | $($md[$a][3]) | $($md[$a][4]) "
    echo "+---+---+---+---+---+"


    }

     

     

     
     
    $f=@(,@(,@(,@())))

    $f[0][0][0]=1
    $f[0][0][0]=1
    $f



     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     


      

    No comments:

    Post a Comment

    #112: How to handle xml document in Powershell?

     In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...