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 - Variable assignment gone bad

Reply
 
Old 07-09-2007   #1 (permalink)
HipHopHank


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Variable assignment and clone or objet reference PowerShell
lab assignment Vista General
Assignment Help PowerShell
Using a variable on the left-side of an assignment PowerShell
How can I ensure that a variable is a built-in powershell variable? 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