Windows Vista Forums

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


    Marco Shaw [MVP] Guest

    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

    > 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

  2. #2


    Jon Guest

    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

    > 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

  3. #3


    Larry__Weiss Guest

    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:

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

  4. #4


    Larry__Weiss Guest

    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:

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

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Comma or point - powershell problem Jens Wachholz PowerShell 3 18 Oct 2009
Search fails if Folder name has comma in it? NickT Vista file management 1 15 Jan 2008
do something for each token in a line separated by comma MaxMad PowerShell 5 09 Jan 2008
Escape comma in dos command from PS script Brian Vallelunga PowerShell 2 28 Jun 2007
Leading Mattress-Maker Spring Air to Rest Easier With Microsoft Dynamics AX Technology Solution z3r010 Vista News 0 06 Jul 2006