Сравнение-объект, не сравнивающий каждый элемент массива - PullRequest
0 голосов
/ 28 августа 2018

Я пытаюсь использовать Compare-Object, чтобы найти элементы, которые находятся в одном массиве ($fileArray), но не в другом ($dictionaryArray). По какой-то причине Compare-Object сравнивает только два последних элемента в fileArray. Вот код, который я пытаюсь запустить.

$dictionary = Get-Content "C:\Users\Joe\Documents\PowerShell Scripts\Work\PCI Numbers\dictionary.txt"
$file = Get-Content "C:\Users\Joe\Documents\PowerShell Scripts\Work\PCI Numbers\file.txt"

$dictionaryArray = @()
$fileArray = @()

$dictionaryLength = $dictionary | Measure-Object -Line
$fileLength = $file | Measure-Object -Line

$i = 0

while($i -lt $dictionaryLength.Lines){
    $name = $dictionary | Select -Index $i
    $i++
    while($dictionary[$i] -ne " " -and $i -lt $dictionaryLength.Lines){
        $dictionaryObject = New-Object -TypeName PSObject
        $dictionaryObject | Add-Member -Name 'PC' -MemberType Noteproperty -Value $name
        $dictionaryObject | Add-Member -Name 'File' -MemberType Noteproperty -Value $dictionary[$i]
        $dictionaryArray += $dictionaryObject
        $i++
    }
    $i++
}

$i = 0

while($i -lt $fileLength.Lines){
    $name = $file | Select -Index $i
    $i++
    while($file[$i] -ne " " -and $i -lt $fileLength.Lines){
        $fileObject = New-Object -TypeName PSObject
        $fileObject | Add-Member -Name 'PC' -MemberType Noteproperty -Value $name
        $fileObject | Add-Member -Name 'File' -MemberType Noteproperty -Value $file[$i]
        $fileArray += $fileObject
        $i++
    }
    $i++
}

$i = 0

Compare-Object -ReferenceObject $fileArray -DifferenceObject $dictionaryArray |
    Where-Object {$_.SideIndicator -eq '<='} |
    ForEach-Object {Write-Output $_.InputObject}

Когда я просто запускаю Compare-Object -ReferenceObject $fileArray -DifferenceObject $dictionaryArray, я получаю вывод

InputObject                                          SideIndicator
@{PC=Joe; File=nopethere}                            &lt;=
@{PC=Joe; File=hi!!!@}                               &lt;=                                              

В fileArray больше элементов, чем это. Когда я запускаю $fileArray, я получаю

PC             File
Eric           I like to
Eric           there
Joe            code because
Joe            hello
Joe            but why?
Joe            *no\thank/ you :c
Joe            nopethere
Joe            hi!!!@

Почему мой код не сравнивает каждый из этих объектов?

EDIT

Вот словарь dictionary.txt

Eric
because
hello there

Joe
but why?
*no\thank/ you :c
nope

Вот файл.txt

Eric
I like to
there

Joe
code because
hello
but why?
*no\thank/ you :c
nopethere
hi!!!@

Я использую свой цикл для создания объектов. Первая строка каждого абзаца - это «ПК» для каждого последующего объекта. Затем каждая строка является «файлом» каждого объекта. Я хочу проверить, какие объекты находятся в fileArray, а не в dictionaryArray. Я ожидаю, что на выходе будет

PC             File
Eric           I like to
Joe            code because
Joe            hello
Joe            nopethere
Joe            hi!!!@

Буду признателен за любую помощь!

...