Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to create a hash table from an array

Reply
 
Old 08-08-2008   #1 (permalink)
RickB


 
 

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
Old 08-08-2008   #2 (permalink)
Alex K. Angelopoulos


 
 

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
Quote:

> 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
Old 08-08-2008   #3 (permalink)
RickB


 
 

Re: How to create a hash table from an array

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

> *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
>
>
>
Quote:

> > 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.
>
Quote:

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

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

> > What function f will result in
>
Quote:

> > 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
Reply

Thread Tools


Similar Threads
Thread Forum
Sorting a Hash Table before outputting it to html PowerShell
Adding data to a hash table PowerShell
Passing hash table by reference PowerShell
Variable as hash table issue PowerShell
How do I read a XML file into a hash table? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46