Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for a key in hashtable and average associated values Powershell

Tags:

powershell

So I have a script where I'm trying to get the test name and store it into a hashtable as the key, then get a high test score and a low test score and store those 2 as the values. Then, I would like to take and allow the user to search for a test name and see the high and low scores. what I have currently is:

$testInfo = @{}
$testName = read-host "Please enter the name of the test"
$testHigh = read-host "Please enter the high score of the test"
$testLow = read-host "Please enter the low score of the test"
$testInfo.Add($testName, $testHigh + " " + $testLow
$search = read-host "Please enter the name of the test you'd like to view the average score of"

This code successfully stores the high and low scores to that test name, but I need a way to find the name of the test with the $search value. Then average the two test scores stored in the values part.

like image 693
OysterMaker Avatar asked Apr 02 '15 21:04

OysterMaker


1 Answers

It depends on how you want to use it, but there are several ways:

$testInfo.ContainsKey($search)

That will return a $true/$false if the key exists.

You can also iterate through the keys:

foreach($key in $testInfo.Keys.GetEnumerator()) {
    $key
}

You could just reference it:

$testInfo[$search]
# or
$testInfo.$search

You can choose the way to reference/use it that best suits your needs.

like image 120
briantist Avatar answered Sep 20 '22 00:09

briantist