2011-05-06 2 views
0

문자열 목록을 통해 중복 수를 계산 한 다음 한 줄에 일치하는 문자열을 파일에 인쇄해야합니다. 여기에 내가 가진 것이 있지만 그 문자열을 한 번만 인쇄하면됩니다. 나의 뇌는 단지 오늘 작동하지그룹 수가 카운트와 중복 됨

Do 
     line = LineInput(1) 
     Trim(line) 
     If line = temp Then 
      counter += 1 
     Else 
      counter = 1 
     End If 
     temp = line 
     swriter.WriteLine(line & " " & counter.ToString) 
     swriter.Flush() 

    Loop While Not EOF(1) 

..

답변

1

당신은 아마 문자열을 계산하기 위해 Dictionary 같은 것을 사용해야합니다. 다음

Dim dict As New Dictionary(Of String, Integer) 
Do 
    line = LineInput(1) 
    line = Trim(line) 
    If dict.ContainsKey(line) Then 
    dict(line) += 1 
    Else 
    dict.Add(line, 1) 
    End If 
Loop While Not EOF(1) 

그리고 사전에 요소를 인쇄

For Each line As String In dict.Keys 
    swriter.WriteLine(line & " " & dict(line)) 
    swriter.Flush() 
Next 
2

또한 사용할 수 LINQ :

Dim dups = From x In IO.File.ReadAllLines("TextFile1.txt") _ 
       Group By line Into Group _ 
       Where Group.Count > 1 _ 
       Let count = Group.Count() _ 
       Order By count Descending _ 
       Select New With { _ 
        Key .Value = x, _ 
        Key .Count = count _ 
       } 

For Each d In dups 
    swriter.WriteLine(String.Format("duplicate: {0} count: {1}", d.Value, d.Count)) 
    swriter.Flush() 
Next