从 csv 导入后的 Powershell 搜索匹配值并更新关联 objects

Powershell after import from csv search for matching value and update assosiate objects

我一直在四处寻找,但没有找到一个例子来完成我想要的。我有一个带有 headers 的导入 CSV 文件,我想找到一个值,当找到时我需要更新数组。

Table

| Pers1ID | Pers1Name |Pers2ID |Pers2Name |Pers1FathID | Pers1FathName|Pers1MothID|Pers1MothName|
| ------- | --------- |------- |--------- |----------- |------------- |---------- |------------ |
|         | Hans A    |        | Cindy B  |            |  Oscar A     |           | Virgin C    |
|         | Rik A     |        |          |            |  Hans A      |           | Cindy B     |

我正在寻找正确的代码,以便为所有汉斯 A 提供相同的 ID,为所有辛迪 B 提供相同的 ID。

原始文件有 3000 行,150 列。

这是我当前的代码:

$personid = 1

$myMultiArray = @(Import-CSV ".\import.csv"  -Encoding UTF8)
$SearchArray = $myMultiArray

$mainarraycount = 0
#starting with the first line of the array I want to start my search and update with match found.
foreach ($mainArrayItem in $myMultiArray) {
    #getting all possible names some could be empty 
    $match = @(($mainarrayitem.Pers1Naam,$mainarrayitem.Pers1VadNaam,$mainarrayitem.Pers1MoeNaam,
                $mainarrayitem.Pers2Naam))

    #removing the empty from my search
    [System.Collections.ArrayList]$Export = @()
    foreach ($value in $match) {
        If (!([string]::IsNullOrEmpty($value))) {
                $Export.add(@($value))        
        }
    }
    $match = $Export
   
    #start search for my first match
    $arrayCount = 0
    foreach ($ArrayItem in $SearchArray) {
        foreach ($item in $match) {
           if ($ArrayItem -Like "*"+$item+"*") {
                 #here I want to add an ID to Pers1ID but also the same ID to when Hans A is Father of Rik A. I tried indexof but that option is not available in this code

                 #$myMultiArray[$arrayCount][($arrayitem[$arrayCount].indexof($item))-1] = $personif
           }
        }
        $arrayCount++
    }
    $mainarraycount++
}

如评论所述,我认为最好创建一个查找哈希表,其中所有键都是人的姓名,相应的值是 ID。

像这样:

# I'm faking the Import-Csv below by using a Here-String.  
# You should do $csv = Import-CSV ".\import.csv"  -Encoding UTF8

$csv = @"
Pers1ID,Pers1Name,Pers2ID,Pers2Name,Pers1FathID,Pers1FathName,Pers1MothID,Pers1MothName
,Hans A,,Cindy B,, Oscar A,,Virgin C
,Rik A,,,,Hans A,,Cindy B
"@ | ConvertFrom-Csv

# create a lookup table
$lookup = @{
    'Hans A'   = 1
    'Cindy B'  = 2
    'Oscar A'  = 3
    'Virgin C' = 4
    'Rik A'    = 5
    # etcetera
}

# now loop through the items in the csv and fill the ID values using the lookup table
foreach ($item in $csv) {
    $item.Pers1ID = $lookup[$item.Pers1Name]
    $item.Pers2ID = $lookup[$item.Pers2Name]
    $item.Pers1FathID = $lookup[$item.Pers1FathName]
    $item.Pers1MothID = $lookup[$item.Pers1MothName]
}

# display on screen
$csv | Format-Table -AutoSize

# write to (new) CSV file
$csv | Export-Csv -Path 'X:\export.csv' -NoTypeInformation -Encoding UTF8

屏幕输出:

Pers1ID Pers1Name Pers2ID Pers2Name Pers1FathID Pers1FathName Pers1MothID Pers1MothName
------- --------- ------- --------- ----------- ------------- ----------- -------------
      1 Hans A          2 Cindy B             3 Oscar A                 4 Virgin C     
      5 Rik A                                 1 Hans A                  2 Cindy B