Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need to display the wordsin the order of which they appear in the text file, then change from a HashTable to a SortedList.</p> <pre><code> Dim hshResults As New Hashtable() Dim lstLinesOfText As List(Of String) = IO.File.ReadAllLines("C:\YourFile.txt").ToList() Dim intLineCursor As Integer = 0 For Each strLine As String In lstLinesOfText Dim lstWords As List(Of String) = strLine.Split(" ").ToList() For Each strWord As String In lstWords ProcessWord(strWord, hshResults, intLineCursor) Next intLineCursor += 1 Next Dim strOutput As String = String.Empty For Each o As DictionaryEntry In hshResults strOutput += CStr(o.Key) &amp; " " Dim lstLinesWhereWordIsFount As List(Of Integer) = CType(o.Value, List(Of Integer)) For Each i As Integer In lstLinesWhereWordIsFount strOutput += CStr(i) &amp; " " Next 'Some cleanup of extra spaces. strOutput = Trim(strOutput) &amp; ControlChars.NewLine Next Private Sub ProcessWord(ByVal strWord As String, ByRef hshResults As Hashtable, ByVal intLineIndex As Integer) Dim lstLinesWhereWordIsFound As List(Of Integer) = (From o As DictionaryEntry In hshResults _ Where CStr(o.Key) = strWord _ Select CType(o.Value, List(Of Integer))).FirstOrDefault() If lstLinesWhereWordIsFound Is Nothing Then 'Add this word. Dim lstNewHashTableValue As New List(Of Integer) lstNewHashTableValue.Add(intLineIndex + 1) 'Indexes in the programming world start at 0. hshResults.Add(CObj(strWord), CObj(lstNewHashTableValue)) Else 'Add the line number for this word. If lstLinesWhereWordIsFound.Count &lt; 5 Then 'Make sure we're not duplicating a line number for this word. If (From i As Integer In lstLinesWhereWordIsFound _ Where i = intLineIndex).Count = 0 Then lstLinesWhereWordIsFound.Add(intLineIndex + 1) hshResults(strWord) = CObj(lstLinesWhereWordIsFound) End If End If End If End Sub </code></pre> <p>Edit: Explaination of Code</p> <p>First, I instantiate a HashTable to store the words and the rows of which they are found. Then I get each line of the text file into a List(of String) object. Iterating through the lines of the text file, I get each word of the line into another List(of String) variable, using the Split method. I send each word of the line through a method (ProcessWord) that will update the HashTable appropriately. Finally, I iterate through all of the key/value pairs in the HashTable to generate the desired output. The ProcessWord method's logic is to first determine whether or not the word already exists in the HashTable. If it does not, add the word and the line number. If it does, then make sure the line count isn't above the frequency of 4 (as requested in your question), make sure that it doesn't put the same line number twice (in case a word is in the same line multiple times), and if all of those conditions are met, add the line number and then update the HashTable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload