Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Variable assignment gone bad

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-09-2007   #1 (permalink)
HipHopHank
Guest


 

Variable assignment gone bad

I am sure that in six months I will think this was a simple question. Right
now it just confuses me. I tried to assign the values from a hash to what I
expected would be an array. Instead I got something completly different, and
I don't seem to be able to access the values stored in this variable. The
same trick works for keys, but it is called a
System.Collections.Hashtable+KeyCollection

Help me Obi Wan Kenobe, you're my only hope. :}

# create a simple hash
PS K:\> $SimpleHash = @{}
PS K:\> $SimpleHash.trout = "fish"
PS K:\> $SimpleHash.tern = "bird"
PS K:\> $SimpleHash.fly = "insect"
PS K:\> $SimpleHash.pear = "fruit"
PS K:\> $SimpleHash

Name Value
---- -----
tern bird
trout fish
fly insect
pear fruit

# check the object type, ok I get that.
PS K:\> $SimpleHash.gettype().fullname
System.Collections.Hashtable

# print just the values
PS K:\> $SimpleHash.values
bird
fish
insect
fruit

# assign the values to a variable, sort of expecting an array.
PS K:\> $mystery = $SimpleHash.values
PS K:\> $mystery
bird
fish
insect
fruit

# check the object type, what the heck?
PS K:\> $mystery.gettype().fullname
System.Collections.Hashtable+ValueCollection


My System SpecsSystem Spec
Old 07-09-2007   #2 (permalink)
hecks@hotmail.co.uk
Guest


 

Re: Variable assignment gone bad

On Jul 9, 10:24 pm, HipHopHank <HipHopH...@discussions.microsoft.com>
wrote:
> I am sure that in six months I will think this was a simple question. Right
> now it just confuses me. I tried to assign the values from a hash to what I
> expected would be an array. Instead I got something completly different, and
> I don't seem to be able to access the values stored in this variable. The
> same trick works for keys, but it is called a
> System.Collections.Hashtable+KeyCollection
>
> Help me Obi Wan Kenobe, you're my only hope. :}
>
> # create a simple hash
> PS K:\> $SimpleHash = @{}
> PS K:\> $SimpleHash.trout = "fish"
> PS K:\> $SimpleHash.tern = "bird"
> PS K:\> $SimpleHash.fly = "insect"
> PS K:\> $SimpleHash.pear = "fruit"
> PS K:\> $SimpleHash
>
> Name Value
> ---- -----
> tern bird
> trout fish
> fly insect
> pear fruit
>
> # check the object type, ok I get that.
> PS K:\> $SimpleHash.gettype().fullname
> System.Collections.Hashtable
>
> # print just the values
> PS K:\> $SimpleHash.values
> bird
> fish
> insect
> fruit
>
> # assign the values to a variable, sort of expecting an array.
> PS K:\> $mystery = $SimpleHash.values
> PS K:\> $mystery
> bird
> fish
> insect
> fruit
>
> # check the object type, what the heck?
> PS K:\> $mystery.gettype().fullname
> System.Collections.Hashtable+ValueCollection


PS C:\> [array]$mystery = $SimpleHash.values
PS C:\> $mystery.gettype().fullname
System.Object[]
PS C:\> $mystery[0]
bird

PS C:\> $SimpleHash.values | %{$mystery += $_}
PS C:\> $mystery.gettype().fullname
System.Object[]
PS C:\> $mystery[1]
fish

PS C:\> $mystery = $SimpleHash.values -as [array]
PS C:\> $mystery.gettype().fullname
System.Object[]
PS C:\> $mystery[2]
insect

No doubt there are other ways to cast the result to an array.

-Hecks

My System SpecsSystem Spec
Old 07-09-2007   #3 (permalink)
hecks@hotmail.co.uk
Guest


 

Re: Variable assignment gone bad

On Jul 9, 10:41 pm, h...@hotmail.co.uk wrote:
> On Jul 9, 10:24 pm, HipHopHank <HipHopH...@discussions.microsoft.com>
> wrote:
>
>
>
> > I am sure that in six months I will think this was a simple question. Right
> > now it just confuses me. I tried to assign the values from a hash to what I
> > expected would be an array. Instead I got something completly different, and
> > I don't seem to be able to access the values stored in this variable. The
> > same trick works for keys, but it is called a
> > System.Collections.Hashtable+KeyCollection

>
> > Help me Obi Wan Kenobe, you're my only hope. :}

>
> > # create a simple hash
> > PS K:\> $SimpleHash = @{}
> > PS K:\> $SimpleHash.trout = "fish"
> > PS K:\> $SimpleHash.tern = "bird"
> > PS K:\> $SimpleHash.fly = "insect"
> > PS K:\> $SimpleHash.pear = "fruit"
> > PS K:\> $SimpleHash

>
> > Name Value
> > ---- -----
> > tern bird
> > trout fish
> > fly insect
> > pear fruit

>
> > # check the object type, ok I get that.
> > PS K:\> $SimpleHash.gettype().fullname
> > System.Collections.Hashtable

>
> > # print just the values
> > PS K:\> $SimpleHash.values
> > bird
> > fish
> > insect
> > fruit

>
> > # assign the values to a variable, sort of expecting an array.
> > PS K:\> $mystery = $SimpleHash.values
> > PS K:\> $mystery
> > bird
> > fish
> > insect
> > fruit

>
> > # check the object type, what the heck?
> > PS K:\> $mystery.gettype().fullname
> > System.Collections.Hashtable+ValueCollection

>
> PS C:\> [array]$mystery = $SimpleHash.values
> PS C:\> $mystery.gettype().fullname
> System.Object[]
> PS C:\> $mystery[0]
> bird
>
> PS C:\> $SimpleHash.values | %{$mystery += $_}
> PS C:\> $mystery.gettype().fullname
> System.Object[]
> PS C:\> $mystery[1]
> fish
>
> PS C:\> $mystery = $SimpleHash.values -as [array]
> PS C:\> $mystery.gettype().fullname
> System.Object[]
> PS C:\> $mystery[2]
> insect
>
> No doubt there are other ways to cast the result to an array.
>
> -Hecks


To be certain, the second one should be:

$SimpleHash.values | %{[array]$mystery += $_}

and the first can also be:

$mystery = @($SimpleHash.values)

I find arrays a bit fiddly in PS, especially when you get a scalar
returned when you were expecting an array. I tend to be over-
cautious and type everything, use the unary comma operator, etc.
Maybe wiser heads around here have more , ahem, disciplined
approaches.

-Hecks

My System SpecsSystem Spec
Old 07-09-2007   #4 (permalink)
Brandon Shell
Guest


 

Re: Variable assignment gone bad

This works for me everytime, although... It also works for me without
[array.]
[array]$mystery = $simplehash.values

<hecks@hotmail.co.uk> wrote in message
news:1184018910.548420.52840@o61g2000hsh.googlegroups.com...
> On Jul 9, 10:41 pm, h...@hotmail.co.uk wrote:
>> On Jul 9, 10:24 pm, HipHopHank <HipHopH...@discussions.microsoft.com>
>> wrote:
>>
>>
>>
>> > I am sure that in six months I will think this was a simple question.
>> > Right
>> > now it just confuses me. I tried to assign the values from a hash to
>> > what I
>> > expected would be an array. Instead I got something completly
>> > different, and
>> > I don't seem to be able to access the values stored in this variable.
>> > The
>> > same trick works for keys, but it is called a
>> > System.Collections.Hashtable+KeyCollection

>>
>> > Help me Obi Wan Kenobe, you're my only hope. :}

>>
>> > # create a simple hash
>> > PS K:\> $SimpleHash = @{}
>> > PS K:\> $SimpleHash.trout = "fish"
>> > PS K:\> $SimpleHash.tern = "bird"
>> > PS K:\> $SimpleHash.fly = "insect"
>> > PS K:\> $SimpleHash.pear = "fruit"
>> > PS K:\> $SimpleHash

>>
>> > Name Value
>> > ---- -----
>> > tern bird
>> > trout fish
>> > fly insect
>> > pear fruit

>>
>> > # check the object type, ok I get that.
>> > PS K:\> $SimpleHash.gettype().fullname
>> > System.Collections.Hashtable

>>
>> > # print just the values
>> > PS K:\> $SimpleHash.values
>> > bird
>> > fish
>> > insect
>> > fruit

>>
>> > # assign the values to a variable, sort of expecting an array.
>> > PS K:\> $mystery = $SimpleHash.values
>> > PS K:\> $mystery
>> > bird
>> > fish
>> > insect
>> > fruit

>>
>> > # check the object type, what the heck?
>> > PS K:\> $mystery.gettype().fullname
>> > System.Collections.Hashtable+ValueCollection

>>
>> PS C:\> [array]$mystery = $SimpleHash.values
>> PS C:\> $mystery.gettype().fullname
>> System.Object[]
>> PS C:\> $mystery[0]
>> bird
>>
>> PS C:\> $SimpleHash.values | %{$mystery += $_}
>> PS C:\> $mystery.gettype().fullname
>> System.Object[]
>> PS C:\> $mystery[1]
>> fish
>>
>> PS C:\> $mystery = $SimpleHash.values -as [array]
>> PS C:\> $mystery.gettype().fullname
>> System.Object[]
>> PS C:\> $mystery[2]
>> insect
>>
>> No doubt there are other ways to cast the result to an array.
>>
>> -Hecks

>
> To be certain, the second one should be:
>
> $SimpleHash.values | %{[array]$mystery += $_}
>
> and the first can also be:
>
> $mystery = @($SimpleHash.values)
>
> I find arrays a bit fiddly in PS, especially when you get a scalar
> returned when you were expecting an array. I tend to be over-
> cautious and type everything, use the unary comma operator, etc.
> Maybe wiser heads around here have more , ahem, disciplined
> approaches.
>
> -Hecks
>


My System SpecsSystem Spec
Old 07-09-2007   #5 (permalink)
hecks@hotmail.co.uk
Guest


 

Re: Variable assignment gone bad

On Jul 9, 11:15 pm, "Brandon Shell" <tshell.m...@mk.gmail.com> wrote:
> This works for me everytime, although... It also works for me without
> [array.]
> [array]$mystery = $simplehash.values
>
> <h...@hotmail.co.uk> wrote in message
>
> news:1184018910.548420.52840@o61g2000hsh.googlegroups.com...
>
> > On Jul 9, 10:41 pm, h...@hotmail.co.uk wrote:
> >> On Jul 9, 10:24 pm, HipHopHank <HipHopH...@discussions.microsoft.com>
> >> wrote:

>
> >> > I am sure that in six months I will think this was a simple question.
> >> > Right
> >> > now it just confuses me. I tried to assign the values from a hash to
> >> > what I
> >> > expected would be an array. Instead I got something completly
> >> > different, and
> >> > I don't seem to be able to access the values stored in this variable.
> >> > The
> >> > same trick works for keys, but it is called a
> >> > System.Collections.Hashtable+KeyCollection

>
> >> > Help me Obi Wan Kenobe, you're my only hope. :}

>
> >> > # create a simple hash
> >> > PS K:\> $SimpleHash = @{}
> >> > PS K:\> $SimpleHash.trout = "fish"
> >> > PS K:\> $SimpleHash.tern = "bird"
> >> > PS K:\> $SimpleHash.fly = "insect"
> >> > PS K:\> $SimpleHash.pear = "fruit"
> >> > PS K:\> $SimpleHash

>
> >> > Name Value
> >> > ---- -----
> >> > tern bird
> >> > trout fish
> >> > fly insect
> >> > pear fruit

>
> >> > # check the object type, ok I get that.
> >> > PS K:\> $SimpleHash.gettype().fullname
> >> > System.Collections.Hashtable

>
> >> > # print just the values
> >> > PS K:\> $SimpleHash.values
> >> > bird
> >> > fish
> >> > insect
> >> > fruit

>
> >> > # assign the values to a variable, sort of expecting an array.
> >> > PS K:\> $mystery = $SimpleHash.values
> >> > PS K:\> $mystery
> >> > bird
> >> > fish
> >> > insect
> >> > fruit

>
> >> > # check the object type, what the heck?
> >> > PS K:\> $mystery.gettype().fullname
> >> > System.Collections.Hashtable+ValueCollection

>
> >> PS C:\> [array]$mystery = $SimpleHash.values
> >> PS C:\> $mystery.gettype().fullname
> >> System.Object[]
> >> PS C:\> $mystery[0]
> >> bird

>
> >> PS C:\> $SimpleHash.values | %{$mystery += $_}
> >> PS C:\> $mystery.gettype().fullname
> >> System.Object[]
> >> PS C:\> $mystery[1]
> >> fish

>
> >> PS C:\> $mystery = $SimpleHash.values -as [array]
> >> PS C:\> $mystery.gettype().fullname
> >> System.Object[]
> >> PS C:\> $mystery[2]
> >> insect

>
> >> No doubt there are other ways to cast the result to an array.

>
> >> -Hecks

>
> > To be certain, the second one should be:

>
> > $SimpleHash.values | %{[array]$mystery += $_}

>
> > and the first can also be:

>
> > $mystery = @($SimpleHash.values)

>
> > I find arrays a bit fiddly in PS, especially when you get a scalar
> > returned when you were expecting an array. I tend to be over-
> > cautious and type everything, use the unary comma operator, etc.
> > Maybe wiser heads around here have more , ahem, disciplined
> > approaches.

>
> > -Hecks


Without explicity casting as an array, I just get an error when trying
to index into it:

PS C:\> $mystery = $SimpleHash.values
PS C:\> $mystery[0]
Unable to index into an object of type System.Collections.Hashtable
+ValueCollec
tion.
At line:1 char:10
+ $mystery[1 <<<< ]

-Hecks

My System SpecsSystem Spec
Old 07-09-2007   #6 (permalink)
klumsy@xtra.co.nz
Guest


 

Re: Variable assignment gone bad

so the Hashtable class .values doesn't return an array but an
Icollection, (which is a general interface).. specifically its
actually returning a

System.Collections.Hashtable+ValueCollection

as your gettype shows..

actually i'd have to run this code in C# to really see the actual type
it is returning as powershell , just like with arrays, can strip
collections apart and stream t he objects in them down the pipeline
one at a time.. Then they get reassembled.. If they can be reassembled
in the Collection type they were, all the better, otherwise they just
exist in an array.

Brandons Example

[array]$mystery = $simplehash.values

is best as it explicity converts the collection into an array. This
forced that $mystery from here on out will ALWAYS be an array.. if you
want to cast it but allow the variable to be more flexible you can
also do

$mystery2 = [array]$simplehash.values


another way is to convert each item in the pipeline to another type,
so that its not the item belonging to hte collection, and therefore
will just be added to an array

$mystery2 = $simplehash.values | % { [string]$_}

here you can see we are casting it as a [string] of course it was a
string before, but before it was a string that happened to be inside a
collection, and now its a new string (copy of string), so not really
"bound" to anything, so its destination is a simple array.

-Karl

My System SpecsSystem Spec
Old 07-10-2007   #7 (permalink)
HipHopHank
Guest


 

Re: Variable assignment gone bad

"klumsy@xtra.co.nz" wrote:
> so the Hashtable class .values doesn't return an array but an
> Icollection, (which is a general interface).. specifically its
> actually returning a
>
> System.Collections.Hashtable+ValueCollection
>
> as your gettype shows..
>
> actually i'd have to run this code in C# to really see the actual type
> it is returning as powershell , just like with arrays, can strip
> collections apart and stream t he objects in them down the pipeline
> one at a time.. Then they get reassembled.. If they can be reassembled
> in the Collection type they were, all the better, otherwise they just
> exist in an array.
>
> Brandons Example
>
> [array]$mystery = $simplehash.values
>
> is best as it explicity converts the collection into an array. This
> forced that $mystery from here on out will ALWAYS be an array.. if you
> want to cast it but allow the variable to be more flexible you can
> also do
>
> $mystery2 = [array]$simplehash.values
>
>
> another way is to convert each item in the pipeline to another type,
> so that its not the item belonging to hte collection, and therefore
> will just be added to an array
>
> $mystery2 = $simplehash.values | % { [string]$_}
>
> here you can see we are casting it as a [string] of course it was a
> string before, but before it was a string that happened to be inside a
> collection, and now its a new string (copy of string), so not really
> "bound" to anything, so its destination is a simple array.
>
> -Karl
>


Thanks to everyone. The answer from Brandon, [array]$mystery =
simplehash.values tells me how to do what I want, but I have to admit I am
also a little concerned that I could so easily create a variable that was
inaccessable.

Thanks to everyone for the help.

HHH


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Assignment Help Arda PowerShell 5 06-09-2008 08:07 AM
CTRL key (re)assignment ricardo.l Vista hardware & devices 2 11-11-2007 02:21 PM
Drive letter assignment smudgers2 Vista General 1 11-02-2007 05:21 AM
Using a variable on the left-side of an assignment Marco Shaw PowerShell 8 12-10-2006 07:07 AM
How can I ensure that a variable is a built-in powershell variable? Sung M Kim PowerShell 7 09-22-2006 06:28 PM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008