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 - Re: Leading comma operator - What does it do, really?

Reply
 
Old 09-15-2009   #1 (permalink)
Marco Shaw [MVP]


 
 

Re: Leading comma operator - What does it do, really?

Good question. I'm not aware of any definitive article.

An easy way to see the difference:
PS>(1,2)|get-member
vs
PS>,(1,2)|get-member

In the first case, each element is passed to get-member. In the second
case, the elements remain grouped together and are passed as a whole to
get-member.

So at first, PowerShell sees the numbers 1 and 2 being passed, while in the
second, PowerShell sees a collection of 1 *and* 2 being passed together.

That being said, I'm not sure what's going on in your last example below.

Marco

"Kevin Buchan" <kevin.buchan@newsgroup[PlsDon'tSpam]sanders.com> wrote in
message news:n1bva5173gsjipm0gbsdimbiahafkv1q78@newsgroup
Quote:

> I'm having trouble finding a definitive article on what exactly the
> leading comma does.
>
> PS C:\> $a
> PS C:\> $a = 1,2,3
> PS C:\> $b = 4,5,6
> PS C:\> ($a + $b).length
> 6
>
> It *seems* to treat what follows it as an element in an array, even if
> it's an array. So instead of creating a new array with all of the
> elements in $a and $b, it creates an array with all of the elements in
> $a and another array element with the entire $b array as that single
> element.
>
> PS C:\> ($a + ,$b).length
> 4
> PS C:\>
> PS C:\> $c = $a + ,$b
> PS C:\> $c[3].GetType().FullName
> System.Object[]
> PS C:\> $c[3]
> 4
> 5
> 6
> PS C:\> $c[3][0]
> 4
>
> When I try it before a string, I get an array with a single element,
> the string.
> PS C:\> $d = ,"Test","AnotherTest"
> PS C:\> $d.GetType().FullName
> System.Object[]
> PS C:\> $d.length
> 2
> PS C:\> $d[0]
> Test
>
>
> OK, all of that seems reasonable to me. But, now here's where my mind
> gets blown...
>
> Why, in this example below, is the first element of $e a
> multi-dimensional array instead of just being a simple array of
> integers?
> PS C:\> $e = ,(1,2,3),4,5,6
> PS C:\> $e.length
> 4
> PS C:\> $e[0].GetType().FullName
> System.Object[]
> PS C:\> $e[0][1] # I expected this to return '2'.
> PS C:\> $e[0][0]
> 1
> 2
> 3
> PS C:\> $e[0][0][1] # What the heck?
> 2
>
>
> --
> Kevin Buchan
> kevin.buchan@newsgroup[nospam]sanders.com

My System SpecsSystem Spec
Old 09-15-2009   #2 (permalink)
Jon


 
 

Re: Leading comma operator - What does it do, really?

Ditto 'good question'.

To paraphrase your example

$e = ,(1,2,3),4,5,6

behaves in the same way as
$e = ,,(1,2,3) + 4,5,6

whereas one would expect it to be semantically equivalent to
$e = ,(1,2,3) + 4,5,6


Possibly a bug ?

--
Jon


"Kevin Buchan" <kevin.buchan@newsgroup[PlsDon'tSpam]sanders.com> wrote in
message news:n1bva5173gsjipm0gbsdimbiahafkv1q78@newsgroup
Quote:

> I'm having trouble finding a definitive article on what exactly the
> leading comma does.
>
> PS C:\> $a
> PS C:\> $a = 1,2,3
> PS C:\> $b = 4,5,6
> PS C:\> ($a + $b).length
> 6
>
> It *seems* to treat what follows it as an element in an array, even if
> it's an array. So instead of creating a new array with all of the
> elements in $a and $b, it creates an array with all of the elements in
> $a and another array element with the entire $b array as that single
> element.
>
> PS C:\> ($a + ,$b).length
> 4
> PS C:\>
> PS C:\> $c = $a + ,$b
> PS C:\> $c[3].GetType().FullName
> System.Object[]
> PS C:\> $c[3]
> 4
> 5
> 6
> PS C:\> $c[3][0]
> 4
>
> When I try it before a string, I get an array with a single element,
> the string.
> PS C:\> $d = ,"Test","AnotherTest"
> PS C:\> $d.GetType().FullName
> System.Object[]
> PS C:\> $d.length
> 2
> PS C:\> $d[0]
> Test
>
>
> OK, all of that seems reasonable to me. But, now here's where my mind
> gets blown...
>
> Why, in this example below, is the first element of $e a
> multi-dimensional array instead of just being a simple array of
> integers?
> PS C:\> $e = ,(1,2,3),4,5,6
> PS C:\> $e.length
> 4
> PS C:\> $e[0].GetType().FullName
> System.Object[]
> PS C:\> $e[0][1] # I expected this to return '2'.
> PS C:\> $e[0][0]
> 1
> 2
> 3
> PS C:\> $e[0][0][1] # What the heck?
> 2
>
>
> --
> Kevin Buchan
> kevin.buchan@newsgroup[nospam]sanders.com
My System SpecsSystem Spec
Old 09-15-2009   #3 (permalink)
Larry__Weiss


 
 

Re: Leading comma operator - What does it do, really?

PS C:\> $e.SyncRoot

helps illuminate the structure of $e
Try, for example

PS C:> $e = ,(1,2),,(3,4),,(5,6)
PS C:> $e.SyncRoot

See also
http://devcentral.f5.com/weblogs/Joe...nraveling.aspx

- Larry


Kevin Buchan wrote:
Quote:

> ...
> Why, in this example below, is the first element of $e a
> multi-dimensional array instead of just being a simple array of
> integers?
> PS C:\> $e = ,(1,2,3),4,5,6
> PS C:\> $e.length
> 4
> PS C:\> $e[0].GetType().FullName
> System.Object[]
> PS C:\> $e[0][1] # I expected this to return '2'.
> PS C:\> $e[0][0]
> 1
> 2
> 3
> PS C:\> $e[0][0][1] # What the heck?
> 2
>
My System SpecsSystem Spec
Old 09-15-2009   #4 (permalink)
Larry__Weiss


 
 

Re: Leading comma operator - What does it do, really?

PS C:> $e = ,(1,2),,(3,4),,(5,6)
can be written as
PS C:> $e = (,(1,2)),(,(3,4)),(,(5,6))

- Larry

Larry__Weiss wrote:
Quote:

> PS C:\> $e.SyncRoot
>
> helps illuminate the structure of $e
> Try, for example
>
> PS C:> $e = ,(1,2),,(3,4),,(5,6)
> PS C:> $e.SyncRoot
>
> See also
> http://devcentral.f5.com/weblogs/Joe...nraveling.aspx
>
> Kevin Buchan wrote:
Quote:

> > ...
>> Why, in this example below, is the first element of $e a
>> multi-dimensional array instead of just being a simple array of
>> integers?
>> PS C:\> $e = ,(1,2,3),4,5,6
>> PS C:\> $e.length
>> 4
>> PS C:\> $e[0].GetType().FullName
>> System.Object[]
>> PS C:\> $e[0][1] # I expected this to return '2'.
>> PS C:\> $e[0][0]
>> 1
>> 2
>> 3
>> PS C:\> $e[0][0][1] # What the heck?
>> 2
>>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
comma delimited text file into database PowerShell
Search fails if Folder name has comma in it? Vista file management
do something for each token in a line separated by comma PowerShell
Escape comma in dos command from PS script PowerShell
Leading Mattress-Maker Spring Air to Rest Easier With Microsoft Dynamics AX Technology Solution Vista News


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