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:
Displaying and accessing elements of Array:
You can access each element of array with [<index_number>]. Square brackets enclosed in index number.
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 -
Output:
True
False
So, you may use it in your scripts like this -
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 -
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
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:
- $x=@()
- $x += "Som"
- $x += "Jack"
- $x += "John"
- $x += "Micky"
Displaying and accessing elements of Array:
You can access each element of array with [<index_number>]. Square brackets enclosed in index number.
- $x[0]
- $x
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 -
- $x -contains "jack"
- $x -contains "xx"
True
False
So, you may use it in your scripts like this -
- if ( $x -contains "jack" )
- {
- Write-Host "jack exits!"
- }
- else
- {
- Write-Host "jack does not exits!!"
- }
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 -
- clear
- $x=@()
- $x += "Som"
- $x += "Som"
- $x += "Som"
- $x += "Ravi"
- $x += "jack"
- Write-Host "Before Deleting element named 'jack' - "
- $x
- $x = $x | where {$_ -ne "jack" }
- Write-Host "After Deleting element named 'jack' - "
- $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