Working with Arrays, Matching and Comparing

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #6069

    by maverick918 at 2013-04-06 12:58:19

    So I'm having this issue I've been researching and I was wondering if anyone has run into this before.

    I have two CSV files that I'm importing into my powershell script

    Lets call them

    $A
    $B

    Both contain one column that match. Depending on this matching column I would like to have whatever is in that column added to a master column. Let me show you.

    $A.Number
    $A.Letter
    $A.Label

    $B.Number
    $B.File
    $B.Size
    $B.Lun

    If $A.Number and $B.Number Match I would like to have another object that looks like this

    $A.Number
    $A.Letter
    $A.Label
    $B.File
    $B.Size
    $B.Lun

    But there are multiple columns like this in each CSV file and $A.Number and $B.Number are sometimes in a random order, however they will always have the same amount of numbers. I've been doing research on this but I haven't found anything that is exactly what I'm looking to do, maybe a nested foreach with an if statement.

    Let me know if you guys have ever had to do anything like this.

    Thanks
    Jake

    by coderaven at 2013-04-06 13:20:09

    Let me see if I can work you up an example.
    $A = Import-Csv -Path ...
    $B = Import-Csv -Path ...

    foreach ($AObj in $A)
    {
    foreach ($BObj in $B)
    {
    if ($AObj.Number -eq $BObj.Number)
    {
    New-PSObject -TypeName PSObject -Property @{ `
    "Number" = $AObj.Number;
    "Letter" = $AObj.Letter;
    "Label" = $AObj.Label;
    "File" = $BObj.File;
    "Size" = $BObj.Size;
    "Lun" = $BObj.Lun;
    }#end PSObject Hashtable
    }#end if
    }#end for B
    }#end for A

    The output should give you the result you are wanting. You can take that output and export-csv.

    by maverick918 at 2013-04-06 13:45:01

    CodeRaven.

    Thank you so much. You.Are.Awesome.

    This worked perfectly.

    by Lembasts at 2013-04-07 16:42:27

    If you sorted the files by number you could use my little join-csv utility...
    http://gallery.technet.microsoft.com/Jo ... 60#content

You must be logged in to reply to this topic.