|
-
Jan 23rd, 2008, 03:19 PM
#1
Thread Starter
New Member
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
-
Jan 23rd, 2008, 03:21 PM
#2
Re: Sorting a txt file with 1st 12 char as keys
Thread Moved from VB FAQ Forum
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Jan 23rd, 2008, 03:26 PM
#3
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/
-
Jan 23rd, 2008, 03:35 PM
#4
Re: Sorting a txt file with 1st 12 char as keys
 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.
-
Jan 23rd, 2008, 05:49 PM
#5
Thread Starter
New Member
Re: Sorting a txt file with 1st 12 char as keys
-
Jan 23rd, 2008, 08:39 PM
#6
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)
-
Feb 6th, 2008, 10:43 AM
#7
Thread Starter
New Member
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
Last edited by rhgarrett; Feb 6th, 2008 at 10:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|