Collections

Arrays

Making my first array

$myarray = 1,2,3,4,5
$myarray
$myarray.length

Looping an array

$primes = @(2,3,5,7,11,13,17,19,23,29)
for($i=0;$i -lt $myarray.length;$i++) {
    write-host "Prime number $i "($primes[$i])
}

Doing something with the array

$myarray = 1,2,3,4,5
foreach ($number in $myarray) { 
    $pow = $number*$number
    write-host "The power of $number is $pow"
}

Pipeline loop (foreach-object can be replaced by % )

$myarray = 1,2,3,4,5
$myarray | foreach-object {
     write-host "Step $_" 
}
(0..5) # makes an array from 0 to 5 
$mybigarr = (0..1000000) # makes a big array with elements 0 to 1000000
Measure-Command {foreach ($number in $mybigarr) { $i = $number }} | select seconds,milliseconds
Measure-Command {$mybigarr  | ForEach-Object { $i = $_ }} | select seconds,milliseconds

Adding a value to an array

$myarray = 1,2,3,4,5
$myarray += 6
$myarray

Check if a value is in an array

$primes = @(2,3,5,7,11,13,17,19,23,29)
3 -in $primes
4 -in $primes

Explicit array

[int[]]$myarray = @(1.2,3,5.6,9)
$myarray
$myarray += "lol"

Just a bonus, an array with numbers from x to y

$oneToTen = (1..10)

Try to make a primary number generation function with argument -max primes

function Make-PrimeArray {
    param (
        [int]$max = 10
    )
    $primes = @()

    $try = 2

    while ($primes.length -lt $max) {
        $isPrime = $true
        for($i=0;$i -lt $primes.length -and $isPrime;$i++) {
            $prime = $primes[$i]

            if (($try%$prime) -eq 0 ) {
                $isPrime = $false
            }
        }

        if ($isPrime) {
            $primes += $try
        }
        $try++
    }

    return $primes
}

Giving it a spin

$primes = Make-PrimeArray -max 100
$primes.length

foreach($i in (0..($primes.length-1))) {
    $prime = $primes[$i]
    write-host "Prime number $i is $prime"
}

Creating an array with predefined length

#1..20
[int[]]$numbers = (1..20)
$numbers
#all zeroes
[int[]]$numbers = New-Object -TypeName int[] -ArgumentList 20
$numbers

Byte array

[byte[]]$bytes = New-Object -TypeName byte[] -ArgumentList 1024

Split / Join

$splitit = "Tom,Alice,Bob"
$splitted = $splitit -split ","    
$splitted
$splitted -join ";"

Hash Tables

Defining a hash tables

$scores = @{ 
    "Alice"=10;
    "Bob"=4;
    "Susan"=8
}
$scores

Accessing a member

$scores["Bob"]
$scores.Set_Item("Bob",2)
$scores["Bob"]

$scores["Bob"] = 3
$scores["Bob"]

Adding a member

$scores.count
$scores.Add("Dan",3)
$scores.count

$scores["Dave"] = 7
$scores

Removing a member

$scores.count
$scores.Remove("Dan")
$scores.count

Looping

foreach ($pair in $scores.GetEnumerator()) {
    write-host $pair.name
    write-host "  "$pair.value
}

Looping alternative

foreach ($key in $scores.Keys) {
    write-host $key
    write-host "  "($scores[$key])
}

Looping without names

foreach ($value in $scores.Values) {
    write-host $value
}

Average score

$total = 0
foreach ($value in $scores.Values) {
    $total += $value
}
write-host ($total/$scores.count)

References: More Info

results matching ""

    No results matching ""