Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. Dim sread As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
  2.  
  3. FileStream
  4.  
  5. Function SummarizeData(filename As String, delimiter As Char) As Dictionary(Of Integer, Integer())
  6. Dim limit As Integer = 0
  7. SummarizeData = New Dictionary(Of Integer, Integer())
  8. Using sr As New IO.StreamReader(filename)
  9. 'Since we don't need the first line for the summary we can read it get _
  10. 'the upper bound for the array, and discard the line.
  11. If Not sr.EndOfStream Then
  12. limit = sr.ReadLine.Split(delimiter).Length - 1
  13. Else : Throw New Exception("Empty File")
  14. End If
  15. Do Until sr.EndOfStream
  16. 'This creates an array of integers representing the data in one line.
  17. Dim line = sr.ReadLine.Split(" "c).Select(Function(x) Integer.Parse(x)).ToArray
  18. 'If the key is already in the dictionary we increment the values
  19. If SummarizeData.ContainsKey(line(0)) Then
  20. For I = 1 To limit
  21. SummarizeData.Item(line(0))(I) += line(I)
  22. Next
  23. Else
  24. 'If not we create a new element using the line as the initial values
  25. SummarizeData.Add(line(0), New Integer(limit) {})
  26. SummarizeData.Item(line(0)) = line
  27. End If
  28. Loop
  29. End Using
  30. End Function
  31.  
  32. Dim results = SummarizeData("data.txt", ","c)
  33. 'If you don't need the results sorted you can gain a few fractions of a second by _
  34. 'removing the Order By clause
  35. IO.File.WriteAllLines("results.txt", (From kvp In results
  36. Order By kvp.Key
  37. Select String.Join(",", kvp.Value)).ToArray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement