Help with hash table

Hi, given the $variable  with 2 properties

Country   City

England   London

USA          Washington

France Paris

When i query an AD  user and get 

$user.city= 'London'

How can i compare it against $variable.city so it equals the $variable.country as England in this case?

I think is with hash tables but i am not sure how to implement it.

Thanks

July 24th, 2013 9:43am

you mean something like this?

$variable = @{
    London='England'
    Washington='USA'
}

#simulating an object returned from ad with a country and city property
$user = new-object psobject -Property @{
    City='London'
    Country=''
}
#display the user before the change
$user

#first validate that the value exists in the hash
if ($variable.($user.city)) {
    #set the value
    $user.country = $variable.($user.city)
}

#display the user after the change
$user

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2013 10:06am

Hi, thanks for your reply.

I  think i haven't explained myself properly...what i have is not a hash table but i thought that was the answer.

I just have a normal object $variable with 2 properties $variable.city, $variable.country.

I want something that returns the $variable.country that matches  the $variable.city with  $user.city 

Thanks

July 24th, 2013 10:43am

Or, create a small custom object, create instances of that object and fill them into an ArrayList. Then, compare your $user.city value with those members of your ArrayList:

$lst = New-Object System.Collections.ArrayList
$source = @'
public class Place {
    public string Country;
    public string City;

    public Place(string country, string city){
        Country = country;
        City = city;
    }
}
'@
Add-Type -TypeDefinition $source

$lst.Add((New-Object Place -ArgumentList ("England", "London")))
$lst.Add((New-Object Place -ArgumentList ("USA", "New York")))
$lst.Add((New-Object Place -ArgumentList ("USA", "Washington")))

function GetUserCountry([String]$city) {
    $lst | % {
        if ( $city -eq $_.City) {
            "User is from " + $_.City + " in " + $_.Country
        }
    }
}
    

    

Use the function as follows: GetUserCountry $user.city

wizend

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2013 10:45am

HiWizend, that would imply creating the array manually?

The lsit is quite big and it changes..

Thanks

July 24th, 2013 10:53am

That might depend on how you get those values of those properties city and country. Maybe, you can programmatically read out those data and put them into some kind of collection for further use.
Free Windows Admin Tool Kit Click here and download it now
July 24th, 2013 11:04am

Could you clarify?  One object in $variable or is it a collection of objects?  I'm assuming a collection of objects with .city and .country.

So, if you have a list of objects with name and city in them, and you want to join them with your user object, you can simply do the following:

#Simulate the $variable list you are using
$variable = @()
$variable += New-Object psobject -Property @{
    City='London'
    Country='England'
}
$variable += New-Object psobject -Property @{
    City='Washington'
    Country='USA'
}


#simulating an object returned from ad with a country and city property
$user = new-object psobject -Property @{
    City='London'
    Country=''
}

#display the user before the change
$user

$user.Country = ($variable |?{ $_.city -eq $user.City} |select -First 1 -ExpandProperty Country)

#display the uer after the change
$user

However, if you want to do these lookups frequently in your code, for example, in a loop, you'll want to convert the list acting as your lookup into a hashtable.   I've documented the technique here: http://powertoe.wordpress.com/2011/03/31/combining-objects-efficiently-use-a-hash-table-to-index-a-collection-of-objects/

For your example, the code would look like this:

#Simulate the $variable list you are using
$variable = @()
$variable += New-Object psobject -Property @{
    City='London'
    Country='England'
}
$variable += New-Object psobject -Property @{
    City='Washington'
    Country='USA'
}


#simulating an object returned from ad with a country and city property
$user = new-object psobject -Property @{
    City='London'
    Country=''
}

#Convert your lookup list to an indexed list (hash table)
$lookup = @{}
foreach ($element in $variable) {$lookup.($element.city) = $element.Country}

#display the user before the change
$user

#first validate that the value exists in the hash
if ($lookup.($user.city)) {
    #set the value
    $user.country = $lookup.($user.city)
}

#display the uer after the change
$user

July 24th, 2013 12:45pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics