Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dim sread As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
- FileStream
- Function SummarizeData(filename As String, delimiter As Char) As Dictionary(Of Integer, Integer())
- Dim limit As Integer = 0
- SummarizeData = New Dictionary(Of Integer, Integer())
- Using sr As New IO.StreamReader(filename)
- 'Since we don't need the first line for the summary we can read it get _
- 'the upper bound for the array, and discard the line.
- If Not sr.EndOfStream Then
- limit = sr.ReadLine.Split(delimiter).Length - 1
- Else : Throw New Exception("Empty File")
- End If
- Do Until sr.EndOfStream
- 'This creates an array of integers representing the data in one line.
- Dim line = sr.ReadLine.Split(" "c).Select(Function(x) Integer.Parse(x)).ToArray
- 'If the key is already in the dictionary we increment the values
- If SummarizeData.ContainsKey(line(0)) Then
- For I = 1 To limit
- SummarizeData.Item(line(0))(I) += line(I)
- Next
- Else
- 'If not we create a new element using the line as the initial values
- SummarizeData.Add(line(0), New Integer(limit) {})
- SummarizeData.Item(line(0)) = line
- End If
- Loop
- End Using
- End Function
- Dim results = SummarizeData("data.txt", ","c)
- 'If you don't need the results sorted you can gain a few fractions of a second by _
- 'removing the Order By clause
- IO.File.WriteAllLines("results.txt", (From kvp In results
- Order By kvp.Key
- Select String.Join(",", kvp.Value)).ToArray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement