Reputation: 319
So if you look at the code below I have a user input a name, address, city, state, and zip of a person. Then I create a key from that name with the function CreateKey that takes their name, splits it up, and takes the 2nd letter and the 2nd to last letter of the first and last name. Then I set that as the key basis for the hashtable that is then created called $hash. Then it takes that key and sets the address's key equal to the key created in the function + "ADD" then stores the value entered for address into the $hash dictionary, it does the same with the rest. So After I have all of those values, how can I make a search to search through the keys that contain that key created from the function. So if I wanted to look up Brandon Gandy, it would look up all the keys that contain "ROAD", which is what the function creates for the key with that specific name. Anyone have an idea?
$count = 1
$key = ""
$hash = @{}
$searchKey = ""
Function CreateKey($name){
$split = $name -split " "
$first = $split[0]
$last = $split[1]
$arrayLen = $split.Length
if ($arrayLen -gt 2)
{
$last = $split[$arrayLen-1]
}
$f = $first[1]
$fl = $first[$first.Length-2]
$l = $last[1]
$ll = $last[$last.Length-2]
$key = $f + $fl + $l + $ll
$key = $key.ToUpper()
return $key
}
Write-host "This program will ask the user for the names and \n some other information about these people!"
while ($count -lt 3){
$name = Read-Host "Please enter the name of person $count"
$address = Read-Host "Please enter the address of person $count"
$city = Read-Host "Please enter the city of person $count"
$state = Read-Host "Please enter the state of person $count"
$zip = Read-Host "Please enter the zip of person $count"
$key = CreateKey $name
$hash[$key] = $name
$hash[$key + "ADD"] = $address
$hash[$key + "CIT"] = $city
$hash[$key + "STA"] = $state
$hash[$key + "ZIP"] = $zip
write-host ($hash | out-string)
$name = ""
$adress = ""
$city = ""
$state = ""
$zip = ""
$count++
}
$search = read-host "Enter a name to look up their details"
$searchKey = CreateKey $search
if ($hash.ContainsKey($searchKey))
{
}
else
{
write-host "The person you entered does not match any of the people in the dictionary."
}
Upvotes: 2
Views: 18897
Reputation: 46710
Not to avoid the whole scenario but it really looks like you would benefit from creating a custom objects here (small step requires a hashtable anyway). Biggest reason is that your CreateKey
is very likely to create duplicates so good chance you will overwrite data.
Keeping slightly with your original code this will allow you to search the $results
array for what you are looking for. This cleans up the need of a complicated key system.
Write-host "This program will ask the user for the names and \n some other information about these people!"
$count = 0
$results = @()
while ($count -lt 3){
$props = @{
name = Read-Host "Please enter the name of person $count"
address = Read-Host "Please enter the address of person $count"
city = Read-Host "Please enter the city of person $count"
state = Read-Host "Please enter the state of person $count"
zip = Read-Host "Please enter the zip of person $count"
}
$results += New-Object -TypeName PsObject -Property $props
$count++
}
$results | Where-Object{$_.Name -eq "Jim Bob"}
Output based on (mostly) keyboard mashing
zip : 1435
state : ?
name : Jim Bob
address : 20 asdfl
city : kansas city
Upvotes: 3
Reputation: 4454
Try something like this:
$search = read-host "Enter a name to look up their details"
$searchKey = CreateKey $search
$foundKeys = @()
$foundKeys = $hash.Keys | % { if($_.contains($searchKey)){$_}}
if ($foundKeys)
{
#now keep in mind that you could have duplicate records. This assumes you don't
write-host "Person found:"
write-host $hash[$searchKey]
write-host $hash[$searchKey + "ADD"]
write-host $hash[$searchKey + "CIT"]
write-host $hash[$searchKey + "STA"]
write-host $hash[$searchKey + "ZIP"]
}
else
{
write-host "The person you entered does not match any of the people in the dictionary."
}
Upvotes: 3