0

I am attempting to use Compare-Object to find elements that are in one array ($fileArray) but not in another ($dictionaryArray). For some reason, Compare-Object is only comparing the last two elements in fileArray. Here is the code I am trying to run.

$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}

When I simply run Compare-Object -ReferenceObject $fileArray -DifferenceObject $dictionaryArray, I get the output

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

There are more items than this in the fileArray. When I run $fileArray, I get

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!!!@

Why is my code not comparing each one of these objects?

EDIT

Here is dictionary.txt

Eric
because
hello there

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

Here is file.txt

Eric
I like to
there

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

I use my loop to create objects. The first line of each paragraph is the "PC" for each subsequent object. Then, each line is the "File" of each object. I want to check which objects are in fileArray and not in dictionaryArray. I would expect the output to be

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

Any help would be appreciated!

  • Without knowing the content of dicitionary.txt/array it's difficult to give a hint. Your code seems overcomplicatef to me, why not use a `ForEach($Item in $dictionary){..}` to iterate `$dictionary` –  Aug 28 '18 at 12:45
  • 1
    Please provide sample input and describe not only the result you're getting, but also the result you were expecting. – Ansgar Wiechers Aug 28 '18 at 12:47
  • This looks like a bit of an XY problem - can you perhaps describe 1) what the contents of the txt files look like, and 2) what you're trying to accomplish with your script - looks like you might be over-complicating things a bit here – Mathias R. Jessen Aug 28 '18 at 12:51

0 Answers0