2

I have 3 arrays of objects. They look like this:

  • $arr1, $arr2 : Objects with properties like (ID, filename, size, etc).
  • $arrlink : Objects with properties like (file1ID, file2ID, misc). This links 2 of the above objects

What I need to do is combine them into one array so I can write it to a CSV. The 3 need to be linked: File1ID and File2ID to the ID column in the other 2 arrays. I need to be able to pick what properties I want. Here's an example

$arr1[0] = {1,"c:\filename1.txt",1000....}
$arr1[1] = {2,"c:\filename2.txt",2000....}

$arrlink[0] = {1,2,"aaaa")

Desired Result:

newarray[0] = {"c:\filename1.txt", "c:\filename2.txt", "aaaa"}

I know I can foreach through and do this, but was wondering if there was a simpler/more direct way.

Of course, there are many items in each array, but they all link up in a 1 to 1 relationship.

1
  • Convert the arrays(with objects) to a hashtable with fileID as key. Then foreach your link-array and use the id's in the "link object" to extract the values you need from the hashtable. Commented Apr 17, 2013 at 19:16

1 Answer 1

1

I wound up using this. It saves a lot of typing and doesn't need to be modified if I modify the underlying objects (as long as I leave the linked fields alone).

$arrOutput = @()

foreach($a in $arrMatch)
    {
    $objOut = New-Object PsObject

    $a.psobject.properties | % { $objOut | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value}
    $arrS1[$a.file1].psobject.properties | % { $objOut | Add-Member -MemberType $_.MemberType -Name ($_.Name + "_1")  -Value $_.Value}
    $arrS2[$a.file2].psobject.properties | % { $objOut | Add-Member -MemberType $_.MemberType -Name ($_.Name + "_2") -Value $_.Value}
    $arrOutput+= $objOut
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.