Windows Vista Forums

How to create a hash table from an array

  1. #1


    RickB Guest

    How to create a hash table from an array

    I'm looking for the simplest way to turn an array of custom objects
    into a hashtable with some particular property of the custom object
    being the key.

    So, given some function that emits an array of objects

    function myArray {
    1..10|
    %{$a=$_;""|
    select-object name,even,sqr|
    %{$_.name = "a$a"
    $_.even = [int]($a/2)*2 -eq $a
    $_.sqr = $a*$a
    $_}}}

    What function f will result in

    PS 1> $h = f $(myArray) name
    PS 2> $h.a3.even
    False

      My System SpecsSystem Spec

  2. #2


    Alex K. Angelopoulos Guest

    Re: How to create a hash table from an array

    function ArrayToHash
    {
    Param($array, $indexproperty)
    $hash = @{}
    $array | %{$hash[$_.$indexproperty] = $_}
    $hash
    }

    This should turn any enumerable collection into a hash using the named
    property as an index. For example, you can collect filesystem objects from
    the current location and use their FullName property as the index like this:

    ArrayToHash (gci) FullName

    or, to collect them into a variable:

    $h = ArrayToHash (gci) FullName



    "RickB" <rbielaws@xxxxxx> wrote in message
    news:9853d408-c430-410b-a32c-d34ec51a51cc@xxxxxx

    > I'm looking for the simplest way to turn an array of custom objects
    > into a hashtable with some particular property of the custom object
    > being the key.
    >
    > So, given some function that emits an array of objects
    >
    > function myArray {
    > 1..10|
    > %{$a=$_;""|
    > select-object name,even,sqr|
    > %{$_.name = "a$a"
    > $_.even = [int]($a/2)*2 -eq $a
    > $_.sqr = $a*$a
    > $_}}}
    >
    > What function f will result in
    >
    > PS 1> $h = f $(myArray) name
    > PS 2> $h.a3.even
    > False

      My System SpecsSystem Spec

  3. #3


    RickB Guest

    Re: How to create a hash table from an array

    On Aug 8, 3:44*pm, "Alex K. Angelopoulos" <aka(at)mvps.org> wrote:

    > *function ArrayToHash
    > {
    > Param($array, $indexproperty)
    > $hash = @{}
    > $array | %{$hash[$_.$indexproperty] = $_}
    > $hash
    >
    > }
    >
    > This should turn any enumerable collection into a hash using the named
    > property as an index. For example, you can collect filesystem objects from
    > the current location and use their FullName property as the index like this:
    >
    > ArrayToHash (gci) FullName
    >
    > or, to collect them into a variable:
    >
    > $h = ArrayToHash (gci) FullName
    >
    > "RickB" <rbiel...@xxxxxx> wrote in message
    >
    > news:9853d408-c430-410b-a32c-d34ec51a51cc@xxxxxx
    >
    >
    >

    > > I'm looking for the simplest way to turn an array of custom objects
    > > into a hashtable with some particular property of the custom object
    > > being the key.
    >

    > > So, given some function that emits an array of objects
    >

    > > function myArray {
    > > 1..10|
    > > *%{$a=$_;""|
    > > *select-object name,even,sqr|
    > > *%{$_.name = "a$a"
    > > * *$_.even = [int]($a/2)*2 -eq $a
    > > * *$_.sqr = $a*$a
    > > * *$_}}}
    >

    > > What function f will result in
    >

    > > PS 1> $h = f $(myArray) name
    > > PS 2> $h.a3.even
    > > False- Hide quoted text -
    >
    > - Show quoted text -
    Wonderfull
    That was so much help
    Thanks!

      My System SpecsSystem Spec

How to create a hash table from an array

Similar Threads
Thread Forum
Re: Hash Table not being returned properly
I haven't tried your code... By messed up, do you mean where it shows "FALSE"? Marco
PowerShell
Hash Table not returning properly in Function
Hi, I have an issue where I am trying to return the contents of a hash table from a function into my script. I am using two functions: The main...
PowerShell
Sorting a Hash Table before outputting it to html
I have a script that reads a file that contains user log in and log out data. I have a PS script that reads this on the fly for the current...
PowerShell
Adding data to a hash table
I have imported a csv using import-csv $clcode = import-csv $csvlocation | select clcode $clcode results are a list of 6 digit numbers, for the...
PowerShell
Passing hash table by reference
Hi, Could someone show me the correct way to pass a hash table by reference to a function in powershell so i can access the data populated in...
PowerShell
Variable as hash table issue
I've got the "space invaders" PS script on the net as an example, and I saw we can declare variable as has table to group similar variable. ...
PowerShell
How do I read a XML file into a hash table?
Examples needed. Thanks!
PowerShell