Array Union functionality

Nandan Hegde 36,716 Reputation points MVP Volunteer Moderator
2022-09-06T13:12:07.817+00:00

Have the requirement wherein there are 2 outputs which we need to do a union of below expression into an Array

a) $PipelineDataset.Activities.Name

b) ([regex]::Matches((Get-Content $TempFilePath),$pattern).groups|Where-Object Name -EQ '1').Value

238150-image.png

    IF ($PipelineDataset.Activities.Name.GetType().Name -eq 'String')  
    {$OuterActivityNames=$PipelineDataset.Activities.Name.Split(",")}  
    else  
    {$OuterActivityNames=$PipelineDataset.Activities.Name}  
  
  
$ActivitiesName= $OuterActivityNames + ([regex]::Matches((Get-Content $TempFilePath),$pattern).groups|Where-Object Name -EQ '1').Value  

There are scenarios wherein sometime there is only 1 value returned for this output : $PipelineDataset.Activities.Name thereby making it as a String type. So I am using the If else activity to convert it into array. Is that the right approach ?

Also, for the union I am using + operator and in case if there is no value present for ([regex]::Matches((Get-Content $TempFilePath),$pattern).groups|Where-Object Name -EQ '1').Value , it is still appending a value in the output array. example : lets say the 1st gives 1 element and 2nd expression has no value, the length of the overall array is coming as 2.

So how to avoid it?

Like below example :

CLS  
$X="1"  
$X.GetType()  
  
  
  
@($x).GetType()  
$y=''  
  
  
  
$z= @($x)+@($y)  
$z.GetType()  
$z.count  

238243-image.png

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 48,026 Reputation points
    2022-09-06T18:31:15.48+00:00

    The result of your code isn't (or at least, may not be) a union of two arrays. A union should be a list of all the unique items in the arrays. Your code is simply adding the contents of one array to another array. In other words, if you had two lists: 1,3,5,6 and 1,2,3,4,7, the union would be 1,2,3,4,5,6,7 -- not 1,2,3,3,4,5,6,7.

    Here's the way I would do this. I offered two alternative ways of producing the union. Either way works.

    [array]$OuterActivityNames = @()  
    if ($PipelineDataset.Activities.Name.GetType().Name -eq 'String'){  
        $OuterActivityNames=$PipelineDataset.Activities.Name.Split(",")  
    }  
    else{  
        $OuterActivityNames=$PipelineDataset.Activities.Name  
    }  
      
    [array]$ActivitiesName= ( [regex]::Matches( (Get-Content $TempFilePath), $pattern).groups | Where-Object Name -EQ '1' ).Value  
      
    # To get the union of the two arrays you can do this  
    # to produce an unsorted list  
    $union = @{}  
    $u = ${}  
    ForEach ($o in $OuterActivityNames){  
        $union[$o] = 1  
    }  
    ForEach ($a in $ActivitiesName){  
        $union[$a] = 1  
    }  
    $union.keys  
      
    # Or, if you prefer, produce a sorted list  
    $u = @()  
    $u = ($OuterActivityNames + $ActivitiesName) | Sort-Object -Unique  
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.