0

別のテキスト ファイルに含まれる値をテキスト ファイルで検索したい。結果には、リスト 2 に含まれていない値が表示されます

Public Class Form1

    Const TEST1 = "\\folder\compare\list1.txt"
    Const TEST2 = "\\folder\compare\list2.txt"

    Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    'Declare two dictionaries. The key for each will be 
    ' the text from the input line up to,
    'but not including the first ",". 
    ' The valus for each will be the entire input line.

    Dim file1 As New HashSet(Of String) '!
    'Dim file1 As New Dictionary(Of String, String)
    Dim file2 As New Dictionary(Of String, String)

    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        Dim part() As String = line.Split(",")
        If Not file1.ContainsKey(part(0)) Then file1.Add(part(0), line)

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",")
        If Not file2.ContainsKey(part(0)) Then file2.Add(part(0), line) '!
    Next

    AddText("The following lines from " & TEST2 & " are also in " & TEST1)

    For Each key As String In file2.Keys
        If file1.Contains(key) Then
            AddText(file2(key))
        End If
    Next
    Dim keysInList1ThatAreNotInList2 = file1.Except(file2.Keys).ToList '!



    Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
    Dim str = String.Join(vbCrLf, values)
    AddText("ID should not be in this list" & str)

    End Sub

    Private Sub AddText(ByVal text As String)
        txtResults.Text &= text & vbCrLf
    End Sub

End Class

別のテキスト ファイルに含まれる値をテキスト ファイルで検索したい。結果には、リスト2に含まれていない値が表示されます。現時点では、値が一意である限り機能します。大きなテキスト ファイルで特定の値を検索したい。

4

1 に答える 1

0

解決策は最初の出現のみを探しているように見えるため、file1、file2 を作成するときに重複を無視する必要があります

Public Class Form1

    Const TEST1 = "\\folder\compare\list1.txt"
    Const TEST2 = "\\folder\compare\list2.txt"

    Private Sub Button1_Click(ByVal sender As System.Object, _ 
              ByVal e As System.EventArgs)       Handles Button1.Click
    'Declare two dictionaries. The key for each will be 
    ' the text from the input line up to,
    'but not including the first ",". 
    ' The valus for each will be the entire input line.

    Dim file1 As New Dictionary(Of String, String)
    Dim file2 As New Dictionary(Of String, String)

    For Each line As String In System.IO.File.ReadAllLines(TEST1)
        Dim part() As String = line.Split(",") 
        If Not file1.ContainsKey(part(0)) Then  file1.Add(part(0), line)'!

    Next

    For Each line As String In System.IO.File.ReadAllLines(TEST2)
        Dim part() As String = line.Split(",") 
        If Not file2.ContainsKey(part(0)) Then  file2.Add(part(0), line)'!

    Next

    AddText("The following lines from " & TEST2 & " are also in " & TEST1)

    For Each key As String In file2.Keys
        If file1.ContainsKey(key) Then
            AddText(file2(key))
        End If
    Next
    Dim keysInList1ThatAreNotInList2 = file1.Keys.Except(file2.Keys).ToList

    Dim values = From key In keysInList1ThatAreNotInList2 Select file1(key)
    Dim str = String.Join(vbCrLf, values)
    AddText(str)

End Sub

Private Sub AddText(ByVal text As String)
     txtResults.Text &= text & vbCrLf
End Sub

混乱を避けるために、元の回答を削除しました。これは新しい完全なものです。

于 2013-11-11T09:16:02.697 に答える