Skip to content

Deal with empty (and $Null) inputs #19

@iRon7

Description

@iRon7

Considered how to deal with $Null and empty array inputs:

  • @() |Join $Object
    This situation could potentially occur from a chained (inner) join.
    But at the other hand, could useful as initial "object" in an loop: ... |% { $a = $Null } { $a = $a |Join $_ }
  • $Object |Join $Null or $Object |Join @()

Current situation:

PS C:\> $Null |Join $Employee |Ft

Id     Name               Country            Department                 Age      ReportsTo
--     ----               -------            ----------                 ---      ---------
{1, 1} {Aerts, Aerts}     {Belgium, Belgium} {Sales, Sales}             {40, 40} {5, 5}
{2, 2} {Bauer, Bauer}     {Germany, Germany} {Engineering, Engineering} {31, 31} {4, 4}
{3, 3} {Cook, Cook}       {England, England} {Sales, Sales}             {69, 69} {1, 1}
{4, 4} {Duval, Duval}     {France, France}   {Engineering, Engineering} {21, 21} {5, 5}
{5, 5} {Evans, Evans}     {England, England} {Marketing, Marketing}     {35, 35} {, }
{6, 6} {Fischer, Fischer} {Germany, Germany} {Engineering, Engineering} {29, 29} {4, 4}

PS C:\> @() |Join $Employee |Ft

Id     Name               Country            Department                 Age      ReportsTo
--     ----               -------            ----------                 ---      ---------
{1, 1} {Aerts, Aerts}     {Belgium, Belgium} {Sales, Sales}             {40, 40} {5, 5}
{2, 2} {Bauer, Bauer}     {Germany, Germany} {Engineering, Engineering} {31, 31} {4, 4}
{3, 3} {Cook, Cook}       {England, England} {Sales, Sales}             {69, 69} {1, 1}
{4, 4} {Duval, Duval}     {France, France}   {Engineering, Engineering} {21, 21} {5, 5}
{5, 5} {Evans, Evans}     {England, England} {Marketing, Marketing}     {35, 35} {, }
{6, 6} {Fischer, Fischer} {Germany, Germany} {Engineering, Engineering} {29, 29} {4, 4}

PS C:\> $Employee |Join $Null |Ft
InvalidOperation: C:\Join.ps1:345
Line |
 345 |in $Keys) { $Properties.Add($Key, $Object.psobject.properties[$Key].V …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.


Id Name  Country Department Age ReportsTo
-- ----  ------- ---------- --- ---------
 1 Aerts Belgium Sales       40         5

PS C:\> $Employee |Join @() |Ft
InvalidOperation: C:\Join.ps1:454
Line |
 454 |                  ([ref]$RightKeys).Value = $Right.get_Keys()
     |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

MethodInvocationException: C:\Join.ps1:371
Line |
 371 |                      if (!$Expressions.Contains($Key)) {
     |                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "Contains" with "1" argument(s): "Value cannot be null. (Parameter 'key')"

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions