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?
Hello, I make from my English CD MP3 files with Audiograbber. C:\Program Files\audiograbber\English If I open the Vista Explorer (Home Pr.),...
Vista General
The pipeline is empty
Why can't I pipe this through the pipeline? PS C:\>$procs = gps PS C:\>foreach ($obj in $procs) {if ($obj.Responding -eq "True")...
PowerShell
Re: Unable to cast COM object of type 'ADODB.RecordsetClass' to class type 'System.Object[]'
"Scotty" <matthieu.sarthou@xxxxxx> schreef in bericht news:1b139922-a0c7-496c-b58d-cbe03f53ec7c@xxxxxx Hello Scotty just change Dim tmp() as...
.NET General
file folder icons display non-empty folders as empty
In using expression web to open a folder the icon shows the folder as empty. When opened a list of files shows. Picture folders show only generic...
Vista file management
Display actual file type, not description of the type
Is it possible, in Windows Explorer Search, to display the actual type, rather than the description of the type? eg .mp3 rather than "MPEG...
Vista file management
Display the actual type, rather than the description of the type
Is it possible, in Windows Explorer Search, to display the actual type, rather than the description of the type? eg .mp3 rather than "MPEG...
Vista General
Unable to find type [Drawing.Image]: make sure that the assembly containing this type is loaded
what must i always do to avoid this common error : make sure that the assembly containing this type is loaded ?
PowerShell