0
 cls
$logFile = "C:\test\output1.txt"
Function LogWrite
{
   Param ([string]$logstring)

   Add-content $Logfile -value $logstring
}
LogWrite "DocumentID|Correct|Wrong|UDI|Number of Errors|Line Number"
LogWrite "------------------------------------------"
$file = "C:\test\test\Birth records evt logging.txt"
$pattern = "^(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(COB Reviewed)$"
$pattern2 = "^(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(DocSecID)$"
$pattern3 = "^(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)`t(.*)$"



$errorCountTotal = 0
$linecount = 0
$line2Count = 0
Get-Content $file|

ForEach-Object{
$errorCountLine = 0
$linecount++
$transposition = $false
   if($_ -match $pattern){


    }elseif($_ -match $pattern2){




    }elseif($_ -match $pattern3){
    $line2Count++



            if($matches[6].Length -eq $matches[7].length){
                 $wrong = $matches[6]
                 $correct = $matches[7]
                 $documentID = $matches[3]
                 $UDI = $matches[8]

                $a = [char[]]$Matches[6]
                $b = [char[]]$matches[7]


               # for($i = 0; $i -lt $a.Length; $i++){

                   # for($x = 1; $x -lt $a.Length; $x++){
                       # if($a[$i] -eq $b[$i+$x] -and $a[$i+$x] -eq $b[$i]){
                         #   if($a[$i] -eq $a[$i+$x]){
                           # write-host "same letter"
                         #   }else{

                               # $errorCountLine += 2

                           # }
                       # }

                   # }


                #}





                #Compare-Object $a $b |Format-List |Out-File "C:\test\test3.txt"

                $errorCountLine += (@(Compare-Object $a $b -SyncWindow 0).count /2)

                $errorCountTotal +=$errorCountLine
                Write-Host $matches[6] " - " $matches[7] " - " $errorCountLine " - " $linecount
                Write-Host $errorCountTotal
                LogWrite "$documentID|$wrong|$correct|$UDI|$errorCountLine|$linecount"
            }else{
                $a = [char[]]$Matches[6]
                $b = [char[]]$matches[7]

                for($i = 0; $i -lt $a.Length; $i++){

                    for($x = 1; $x -lt $a.Length; $x++){
                        if($a[$i] -eq $b[$i+$x] -and $a[$i+$x] -eq $b[$i]){
                            if($a[$i] -eq $a[$i+$x]){
                           # write-host "same letter"
                            }else{

                                $errorCountLine += 2

                            }
                        }

                    }


                }

                $diffL = [math]::Abs($Matches[7].Length - $Matches[6].Length)
                $errorCountLine = (((@(Compare-Object $a $b).count-$diffL) /2) + $diffL)
                $test = @(Compare-Object $a $b).count
                $errorCountTotal += $errorCountLine

                Write-Host $matches[6] " - " $matches[7] " - " $errorCountLine " - " $linecount
                $wrong = $matches[6]
                $correct = $matches[7]
                $documentID = $matches[3]
                $UDI = $matches[8]
                LogWrite "$documentID|$wrong|$correct|$UDI|$errorCountLine|$linecount"
                Write-Host $errorCountTotal

            }


    }



}

Write-Host $line2Count #number of lines that the program looks at.  passes through pattern3.
LogWrite `n
LogWrite "The total number of errors is $errorCountTotal"

I need to compare the contents of two Strings with this program. Above is what i have so far. The only problem is it tells me whether or not the two strings match or not(0 or -1) character by character in the string. Midred and Mildred would come up as 5 errors when in fact it needs to be only 1 error. i cannot just compare the strings as a whole either because there could be multiple errors in a string. any ideas would be great.

2 Answers 2

1

Try using Compare-Object on the character arrays:

$a = [char[]]'Mildred'
$b = [char[]]'Midred'

Compare-Object $a $b


                                                           InputObject SideIndicator                                                        
                                                           ----------- -------------                                                        
                                                                     l <=                   

@(Compare-Object $a $b).count

1
9
  • This works for the example i gave but it returns two if there is an incorrect character ex. Hildred and Mildred returns a 2 when really it should also return a 1
    – Peter3
    Apr 4, 2014 at 17:24
  • Not sure how it's supposed to know if there's only one. If one says Sean and the other says Jean, and the name is actually Dean then there really are 2 errors.
    – mjolinor
    Apr 4, 2014 at 17:43
  • Just did some additional testing, and this might not be a good solution anyway. It won't catch transposed characters. You're probably looking at writing a custom function with the exact matching rules you want.
    – mjolinor
    Apr 4, 2014 at 17:49
  • This wasn't perfect but it got me to where i needed to go. Thanks a lot.
    – Peter3
    Apr 4, 2014 at 18:34
  • This doesn't account for mixed up letters.
    – Peter3
    Apr 4, 2014 at 19:48
0

Why not just compare the two strings, e.g.

$matches[6] -ne $ matches [7]

?

1
  • then it either passes or doesn't pass. what happens if there are two errors/differences in the strings ex. apdla and apple that would need to count for two errors.
    – Peter3
    Apr 4, 2014 at 16:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.