Sorting a txt file with 1st 12 char as keys
OK, this VB 2003 is way different than programming in "C" 15 years ago.
Now a lawyer, I'm programming to pass time. I tried IO.File.ReadAllLines, however it's not in ver 2003.
This first 12 characters of a record in my txt file is what I want to sort on. A typical record starts like this "01E118750216. . ." all ASCII with a CRLF to end the record. The file could be big with 2000 records.
Russell
Re: Sorting a txt file with 1st 12 char as keys
Thread Moved from VB FAQ Forum
Re: Sorting a txt file with 1st 12 char as keys
Are the first 12 character unique?
If programming .NET is a hobby for you, check out latest express versions: http://www.microsoft.com/express/
Re: Sorting a txt file with 1st 12 char as keys
Quote:
Originally Posted by wild_bill
Are the first 12 character unique?
I guess it doesn't matter since vb7 doesn't include a sorted dictionary object
vb Code:
Dim data As New DataTable
data.Columns.Add("key")
data.Columns.Add("value")
Dim sr As New IO.StreamReader("c:\test.txt")
Dim line As String = sr.ReadLine
While Not line Is Nothing
data.Rows.Add(New String() {line.Substring(0, 12), line.Substring(12)})
line = sr.ReadLine
End While
sr.Close()
For Each dr As DataRow In data.Select("", "key")
'do what ever with the sorted results
Next
The next time I'm arrested for exposing myself in public, I'll know who to call.
Re: Sorting a txt file with 1st 12 char as keys
Re: Sorting a txt file with 1st 12 char as keys
vb.net Code:
Dim reader As New IO.StreamReader("file path here")
Dim fileContents As String = reader.ReadToEnd()
reader.Close()
Dim lines As String() = System.Text.RegularExpressions.Regex.Split(fileContents, Environment.NewLine)
Dim upperBound As Integer = lines.GetUpperBound(0)
Dim keys(upperBound) As String
Dim line As String
For index As Integer = 0 To upperBound Step 1
line = lines(index)
keys(index) = line.Substring(0, Math.Min(12, line.Length))
Next index
Array.Sort(keys, lines)
Re: Sorting a txt file with 1st 12 char as keys
Howdy,
I wish to THANK everyone for their posts. I didn't follow exactly what y'all (I'm from the Republic of Texas) suggested. However, it pointed me in the right direction with the right subroutines. Now, I'm pretty busy at work and have litle time to devote to my "pet" projects. The following code is what I wrote from y'alls(Texan draw) suggestions. Not as eloquent or efficient, but functional. I like functional. Basically, I read my txt data into a string array, Parse the array into two other arrays, Sort, rejoin the the orignal sorted arrays and write it.
Private Sub SortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortButton.Click
Dim data As New DataTable
Dim clTodayDate As Date = Date.Now
Dim iTodayMonth As Integer = clTodayDate.Month
Dim ThisMonthStreamReader As StreamReader
Dim stDayEventLine As String
Dim starEventData() As String ' includes keys and actual event
' "st" is a String and "ar" in an Array so the prefix "star" is a string array
Dim iUpperBoundIndex As Integer = 0
Dim stTempPath As String = "c:\program files\today\temp.txt"
Try
' create a temp file
Dim swTempEvent As StreamWriter = File.CreateText(stTempPath)
' Open this month
ThisMonthStreamReader = File.OpenText(GetPath(iTodayMonth))
stDayEventLine = ThisMonthStreamReader.ReadLine()
While Not stDayEventLine = Nothing
starEventData(iUpperBoundIndex) = stDayEventLine
iUpperBoundIndex += 1
stDayEventLine = ThisMonthStreamReader.ReadLine()
End While
Dim starKeys(iUpperBoundIndex) As String ' Keys of the Event
Dim starEvent(iUpperBoundIndex) As String ' Actual Event
For index As Integer = 0 To iUpperBoundIndex Step 1
starKeys(index) = starEventData(index).Substring(0, 11)
starEvent(index) = starEventData(index).Substring(12)
Next index
Array.Sort(starKeys, starEvent)
For index As Integer = 0 To iUpperBoundIndex Step 1
starEventData(index) = starKeys(index) + starEvent(index)
swTempEvent.WriteLine(starEventData(index))
Next index
File.Copy(stTempPath, GetPath(iTodayMonth), True)
Catch ex As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(ex.Message)
End Try
ThisMonthStreamReader.Close()
File.Delete(stTempPath)
End Sub
______________________________
I think this will work, however, I have not had time to test it.
rhgarrett