|
-
Jan 15th, 2012, 06:02 AM
#1
Thread Starter
New Member
selecting a random number of lines from a text file.
Hi,
Im making a text based game that i need to randomly select different numbers of lines from a text file then pass them to a list box.
The text file is made in the following format:
20058,v,o,ED,95
20059,v,o,TI,95
20060,v,o,TI,95
20061,v,o,SL,95
First the code needes to identify the 4th element then randomly select lines from lines that contain that element.
The code needs to work out how many lines have a matching text to the 4th array then randomly select lines from that total no need for a maximum limit to that number.
The same line must only been show the once at a time, but It can then be shown again on a different button click.
The order from the text file must be preserved.
Im stuck on how to randomly select lines from that list.
This is the code i have so far.
Code:
Sub GoClick(sender As Object, e As EventArgs)
ListBox1.Items.Clear()
'clears listbox1
Dim depotsel() As String
depotsel = combodepots1.text.Split(",")
'reads combopepots1 and splits selected itme
Dim code As String
dim name As string
code = depotsel(0)
name = depotsel(1)
textBox1.Text = name
'textbox1 shows selected depots name
Dim SR As StreamReader
SR = New StreamReader("loco.txt")
'assign SR as loco.txt file
Do While SR.Peek <> -1
Dim locos() As String = SR.ReadLine.Split(",")
Dim number As String
Dim brake As String
Dim heat As String
Dim depot As String
Dim points As String
number = locos(0)
brake = locos(1)
heat = locos(2)
depot = locos(3)
points = locos(4)
'splits loco file
If code = depot Then listBox1.Items.Add(number & " " & brake & heat & " " & depot & " " & Points)
'matches loco file to combodepots1 and populates listbox1
Loop
Any help would be much appreciated
Thanks
Carl
-
Jan 15th, 2012, 10:24 AM
#2
Re: selecting a random number of lines from a text file.
Hello Carl,
The following (done in VS2008) reads a text file containing the contents you posted in your question using TextFieldParser class then in a LINQ statement groups Column4 with a count of how many times Column 4 appears in the array of lines read in using TextFieldParser, Column1 and Column4 are selected with the count, you need to add the other columns in.
I used a DataGridView with three columns to display the results, you can use a ListBox or some other control as you wish.
There is nothing in the code for the random requirement, that is for you to work on. So you now have a good starting point to work from.
Implementation
Code:
Private Sub DemoSelectLinesDemo()
Dim Lines As New List(Of DemoClass1)
If Not IO.File.Exists("Document1.txt") Then
MessageBox.Show("Document.txt was not located")
Exit Sub
End If
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Document1.txt")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Lines.Add( _
New DemoClass1 With _
{ _
.Column1 = currentRow(0), _
.Column2 = currentRow(1), _
.Column3 = currentRow(2), _
.Column4 = currentRow(3), _
.Column5 = currentRow(4) _
} _
)
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
Console.WriteLine(ex.Message)
End Try
End While
End Using
'
' Group identical column 4 items
' Return Column1, Column 4 plus the total times Column 4
' value appears. You need to add the other columns in.
'
Dim ItemsGrouped = _
( _
Lines.GroupBy(Function(x) _
New With {Key x.Column4, x.Column1}).Select(Function(group) _
New With {Key .Col1 = group.Key.Column1, .Col4 = group.Key.Column4, .Count = group.Count} _
) _
).ToList
For Each item In ItemsGrouped
DataGridView2.Rows.Add(New Object() {item.Col1, item.Col4, item.Count})
Next
End Sub
Class for above
Code:
Public Class DemoClass1
Private mColumn1 As String
Private mColumn2 As String
Private mColumn3 As String
Private mColumn4 As String
Private mColumn5 As String
Public Property Column5() As String
Get
Return mColumn5
End Get
Set(ByVal value As String)
mColumn5 = value
End Set
End Property
Public Property Column4() As String
Get
Return mColumn4
End Get
Set(ByVal value As String)
mColumn4 = value
End Set
End Property
Public Property Column3() As String
Get
Return mColumn3
End Get
Set(ByVal value As String)
mColumn3 = value
End Set
End Property
Public Property Column2() As String
Get
Return mColumn2
End Get
Set(ByVal value As String)
mColumn2 = value
End Set
End Property
Public Property Column1() As String
Get
Return mColumn1
End Get
Set(ByVal value As String)
mColumn1 = value
End Set
End Property
Public Sub New()
End Sub
End Class
-
Jan 15th, 2012, 01:06 PM
#3
Thread Starter
New Member
Re: selecting a random number of lines from a text file.
Hi,
Thanks for that Kevin, but i do have 2 questions.
1. When i have implemeted that it returns in my listbox object[]array. What do i need to do to fix that?
2. Why is this method beter than what i had done already as it seems to do the same thing but in a different way?
you might be able to tell that im new to this and i dont fully grasp the coding yet. What i have learnt so far has come from searching Youtube/web for tutorials. they only seem to come up with code like i had wrote but nothing like LINQ.
Regards
Carl
-
Jan 15th, 2012, 04:13 PM
#4
Re: selecting a random number of lines from a text file.
 Originally Posted by carlw
Hi,
Thanks for that Kevin, but i do have 2 questions.
1. When i have implemeted that it returns in my listbox object[]array. What do i need to do to fix that?
2. Why is this method beter than what i had done already as it seems to do the same thing but in a different way?
you might be able to tell that im new to this and i dont fully grasp the coding yet. What i have learnt so far has come from searching Youtube/web for tutorials. they only seem to come up with code like i had wrote but nothing like LINQ.
Regards
Carl
Greetings Carl,
In regards to the first question, there is nothing to fix but instead several possible methods to display your data in a ListBox. For example, a class where one propery becomes the DisplayMember, the class might be stored in a List(Of. . . and members of the class obtained by casting the SelectedItem to the proper class. Concerning question 2, never said it was a better method but instead showing how I would perform the task.
In regards to methods used to learn from tutorials, YouTube and any others, they are all subjective to the task you want to perform along with how do they gel with other code. Couple that up with other things such as performance and readability.
For instance, sometimes LINQ will be slower but easier to read then raw procedural code but the slowness is not significant. Over time I would rather have readability unless of course the slowness is extremely noticable by the user. A good example, I create a extremely complex operation which is best described as mimicing MS-Access import functionality for Excel files sent to us from DOJ and various vendors. I decided to use LINQ for many of the operations where I knew the traditional methods would be slightly faster. Once completed all the users said "the operations are very quick". Years later I had to tweak the code and with the LINQ statements made it a breeze. If the LINQ method was a great deal slower we should care less about readability. When I decide on using LINQ I am very prudent rather than simply using LINQ or Lambda or a mixture of both. On a similar note I can say the same thing about LINQ to SQL using DataContext objects vs. traditional SQL methods to obtain data.
you might be able to tell that im new to this and i dont fully grasp the coding yet.
The more you study rather than just using something the better you get at understanding things and realize the power of documentation.
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
|