1.17.3. fejezet, Tömbök, Hash táblák

Tömbök

$array = @("item1", "item2", "item3")
$array | foreach {$_}
 
$numbers = 12, 21, 34, 44
$numbers | foreach {$_}
 
$A = 1..40
$A.getType()
 
$A.Length
 
$caps = [System.Collections.ArrayList]("A", "B", "C", "D")
$caps.RemoveAt(0)
$caps.Remove("B")
 
[System.Collections.ArrayList]$caps = "A", "B", "C", "D", "C", "E"
while ($caps -contains "C") {
    $caps.Remove("C")
}
 
[int32[]]$intA = 1500,2230,3350,4000
$A.getType()
    [System.Collections.ArrayList]$p = Get-Process
    $s = Get-Process | where ProcessName -eq "svchost"
    foreach ($svchost in $s)
    {
        $p.Remove($svchost)
    }

Hash tábla

$hash = @{} 
$hash = @{ ID = 1; Shape = "Square"; Color = "Blue"}
$hash = [ordered]@{ ID = 1; Shape = "Square"; Color = "Blue"}
 
$hash["ID"]
$hash.color
 
$hash.keys
$hash.values
 
$hash.Add("Created","Now")
$hash.Count
$hash.Remove("Updated")
 
write-host("sort by key")
$hash.GetEnumerator() | Sort-Object -Property key

Két lista összehasonlítása

$array1 = "Item 1","Item 2","Item 3","Item 1","Item 12"
$array2 = "Item 1","Item 8","Item 3","Item 9","Item 12" 
Compare-Object $array1 $array2
#InputObject                           SideIndicator
#-----------                           ------------
#Item 8                                =>
#Item 9                                =>
#Item 2                                <=
#Item 1                                <=

Halmaz operátorok

$array = "Item 1","Item 2","Item 3","Item 1","Item 12"
$array -ge "Item 3"
#Item 3
$array -lt "Item 3"
#Item 1
#Item 2
#Item 1
#Item 12
$array -ne "Item 1"
#Item 2
#Item 3
#Item 12 
 
$array -match "Item [0-9]$"
#Item 1
#Item 2
#Item 3
#Item 1
$array -notmatch "Item [0-9]$"
#Item 12
$array -like "*1*"
#Item 1
#Item 1
#Item 12
$array -notlike "*1*" 
#Item 2
#Item 3
"Item 1" -in $array
#True
"Item 1" -notin $array
#False
$array -contains "Item 1"
#True
$array -notcontains "Item 1"
#False
 
#Ismétlődésmentes lista
$array | Select-Object -Unique
#Item 1
#Item 2
#Item 3
#Item 12
 
#Sorbarendezés
$array | Sort-Object -Descending
#Item 3
#Item 2
#Item 12
#Item 1
#Item 1
 
#Kivonás a halmazból
$array2 = "Item 1","Item 2"
$array | ? {$_ -notin $array2}
#Item 3
#Item 12
 
#Két tömb összefűzése
$a = 2,3,4
$b = 5,6,7
$c = $a + $b
$c
#2
#3
#4
#5
#6
#7

Többdimenziós tömbök

$arr=@{}
$arr["my"] = @{}
$arr["my"]["TSHIRTS"] = @{}    
$arr["my"]["TSHIRTS"]["SIZE"] ="XXL"
$arr.david.tshirts.size