Windows Vista Forums

The type and value of an empty pipeline

  1. #1


    DioGenus Guest

    The type and value of an empty pipeline

    I would intuitively think that since the output from a pipeline normally is
    an array, the "output" from an empty pipeline should be the empty array @().

    E.g.

    PS> ( @(1,2) | % { $_ } ).GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    True True Object[] System.Array

    is an array while

    PS> ( @() | % { $_ } ) -eq $null
    True

    Can anyone explain why $null is a better choice than @()? In my scripts, I
    now have to perform extra testing before I can add the output of a pipeline
    to an existing array since the following statement would just add a $null
    element to $myArray if $maybeEmptyArray is empty:

    $myArray += @() + $maybeEmptyArray | % { doProcess $_ }

    I obviously want nothing added to $myArray if $maybeEmptyArray is empty.

      My System SpecsSystem Spec

  2. #2


    stej Guest

    Re: The type and value of an empty pipeline

    Yes, this is a little bit unfortunate.

    But there is a quick fix:
    $maybeEmpty = @( @() | % { $_ } )

    so
    @( @() | % { $_ } ).Length -eq 0

    On May 21, 12:35*pm, DioGenus <DioGe...@newsgroup>
    wrote:

    > I would intuitively think that since the output from a pipeline normally is
    > an array, the "output" from an empty pipeline should be the empty array @().
    >
    > E.g.
    >
    > * * PS> ( @(1,2) | % { $_ } ).GetType()
    >
    > * * IsPublic IsSerial Name * * * * * * * * * * * * * * * * * * BaseType
    > * * -------- -------- ---- * * * * * * * * * * * * * * * * * * --------
    > * * True * * True * * Object[] * * * * * * * * * * * * * * * * System.Array
    >
    > is an array while
    >
    > * * PS> ( @() | % { $_ } ) -eq $null
    > * * True
    >
    > Can anyone explain why $null is a better choice than @()? *In my scripts, I
    > now have to perform extra testing before I can add the output of a pipeline
    > to an existing array since the following statement would just add *a $null
    > element to $myArray if $maybeEmptyArray is empty:
    >
    > * * $myArray += @() + $maybeEmptyArray | % { doProcess $_ }
    >
    > I obviously want nothing added to $myArray if $maybeEmptyArray is empty.

      My System SpecsSystem Spec

  3. #3


    DioGenus Guest

    Re: The type and value of an empty pipeline

    Exactly what I needed! Thanks!!! I spent quite a bit of time hunting down
    this "bug" in my code and truly appreciate that I don't have to add an
    explicit test for each such array addition.

    DioGenus

    "stej" wrote:

    > Yes, this is a little bit unfortunate.
    >
    > But there is a quick fix:
    > $maybeEmpty = @( @() | % { $_ } )
    >
    > so
    > @( @() | % { $_ } ).Length -eq 0
    >
    > On May 21, 12:35 pm, DioGenus <DioGe...@newsgroup>
    > wrote:

    > > I would intuitively think that since the output from a pipeline normally is
    > > an array, the "output" from an empty pipeline should be the empty array @().
    > >
    > > E.g.
    > >
    > > PS> ( @(1,2) | % { $_ } ).GetType()
    > >
    > > IsPublic IsSerial Name BaseType
    > > -------- -------- ---- --------
    > > True True Object[] System.Array
    > >
    > > is an array while
    > >
    > > PS> ( @() | % { $_ } ) -eq $null
    > > True
    > >
    > > Can anyone explain why $null is a better choice than @()? In my scripts, I
    > > now have to perform extra testing before I can add the output of a pipeline
    > > to an existing array since the following statement would just add a $null
    > > element to $myArray if $maybeEmptyArray is empty:
    > >
    > > $myArray += @() + $maybeEmptyArray | % { doProcess $_ }
    > >
    > > I obviously want nothing added to $myArray if $maybeEmptyArray is empty.
    >
    > .
    >

      My System SpecsSystem Spec


The type and value of an empty pipeline
Similar Threads
Thread Forum
Vista explorer - Folder empty, but is not empty, why? Vista General
The pipeline is empty PowerShell
file folder icons display non-empty folders as empty Vista file management
Display actual file type, not description of the type Vista file management
Unable to find type [Drawing.Image]: make sure that the assembly containing this type is loaded PowerShell