Combine 2 arrays with a subset of properties

I am trying to combine 2 arrays that have different properties. The first has 3 properties and the second has 6 properties. The First 3 properties are a subset of the second. Depending on the order that I add things (+=) the results are different.

$test1 =@"
prop1, prop2, prop3
1,2,3
4,5,6
7,8,0
"@ | convertfrom-csv

$test2 =@"
prop1, prop2, prop3,prop4, prop5,prop6
a,b,c,d,e,f
g,h,i,j,k,l
m,n,o,p,q,r
"@ | convertfrom-csv

$obj += $test1
$obj += $test2

$obj | FT

prop1 prop2 prop3
----- ----- -----
1     2     3    
4     5     6    
7     8     0    
a     b     c    
g     h     i    
m     n     o    


but if I switch the add I get

$obj += $test2
$obj += $test1

$obj | FT

prop1 prop2 prop3 prop4 prop5 prop6
----- ----- ----- ----- ----- -----
a     b     c     d     e     f
g     h     i     j     k     l
m     n     o     p     q     r
1     2     3
4     5     6
7     8     0

I would like to not have to code to the largest dataset.

How can I accomplish this combination?

 

I have tried to create a array with the defined properties at the start of the script

[array]$obj = “” | select prop1,prop2,prop3,prop4,prop5,prop6

Then I can add either the largest or smallest array first but the output has an extra row (I added ### for visual of emptyness)

prop1 prop2 prop3 prop4 prop5 prop6
----- ----- ----- ----- ----- -----
###   ###   ###   ###   ###   ###
a     b     c     d     e     f
g     h     i     j     k     l
m     n     o     p     q     r
1     2     3
4     5     6
7     8     0

But I have not been able to figure out an easy way to remove that extra row. I understand I could check one properties and if it is null then not display that. I would like to check if all properties are null.

I understand that using arrays vs arraylists are not the best from performance. The datasets that I will be dealing with are small. That and my hope is that once this is working for arrays the same concept could apply to arraylists.

 

Thanks

 

 

Using you example a is, I am not seeing an extra line.

$obj = $null

$test1 =@"
prop1, prop2, prop3
1,2,3
4,5,6
7,8,0
"@ | convertfrom-csv

$test2 =@"
prop1, prop2, prop3,prop4, prop5,prop6
a,b,c,d,e,f
g,h,i,j,k,l
m,n,o,p,q,r
"@ | convertfrom-csv

$obj += $test2
$obj += $test1


$obj | FT

prop1 prop2 prop3 prop4 prop5 prop6
----- ----- ----- ----- ----- -----
a     b     c     d     e     f    
g     h     i     j     k     l    
m     n     o     p     q     r    
1     2     3                      
4     5     6                      
7     8     0                      

You really don’t need that extra $obj += either. You an just do this and the result is the same

$obj = $null

$test1 =@"
prop1, prop2, prop3
1,2,3
4,5,6
7,8,0
"@ | convertfrom-csv

$test2 =@"
prop1, prop2, prop3,prop4, prop5,prop6
a,b,c,d,e,f
g,h,i,j,k,l
m,n,o,p,q,r
"@ | convertfrom-csv

$obj = $test2 + $test1


$obj | FT

prop1 prop2 prop3 prop4 prop5 prop6
----- ----- ----- ----- ----- -----
a     b     c     d     e     f    
g     h     i     j     k     l    
m     n     o     p     q     r    
1     2     3                      
4     5     6                      
7     8     0                      

Don’t worry about my extra ‘$obj = $null’ at the top. I have ‘Set-StrictMode -Version Latest’ in my profile and that prevents using variables that have not been set. As your post is doing.

Even, so, if you have an empty line on the output that is bothering you, just trim it using the normal trim conventions.