Question:
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:
1 2 3 4 5 6 7 |
$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.
Answer:
It depends on how you want to use it, but there are several ways:
1 2 |
$testInfo.ContainsKey($search) |
That will return a $true
/$false
if the key exists.
You can also iterate through the keys:
1 2 3 4 |
foreach($key in $testInfo.Keys.GetEnumerator()) { $key } |
You could just reference it:
1 2 3 4 |
$testInfo[$search] # or $testInfo.$search |
You can choose the way to reference/use it that best suits your needs.