Results 1 to 3 of 3

Thread: Sorting Text

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2004
    Posts
    6

    Sorting Text

    I have been looking at (reading) how to sort text and tryed to implement it into my Text Editor but for some reason it will not work
    It is constantly showing me the same error.
    Here is the Code:
    VB Code:
    1. Dim ln, curline, letter As String
    2.         Dim i, charsInFile, lineCount As Short
    3.         lineCount = 0
    4.         charsInFile = tb1.Text.Length
    5.         For i = 0 To charsInFile - 1
    6.             letter = tb1.Text.Substring(i, 1)
    7.             If letter = Chr(13) Then
    8.                 lineCount += 1
    9.                 i += 1
    10.             End If
    11.         Next i
    12.         ReDim strArray(lineCount)
    13.         curline = 1
    14.         ln = ""
    15.         For i = 0 To charsInFile - 1
    16.             letter = tb1.Text.Substring(i, 1)
    17.             If letter = Chr(13) Then
    18.                 curline = curline + 1
    19.                 i += 1
    20.                 ln = ""
    21.             Else
    22. '==================================================================
    23.                 ln = ln & letter
    24.                 strArray(curline) = 1'<---Here is the prolem!
    25. 'And this is the message I'm getting: Additional information: Index was outside the bounds of the array.
    26. '==================================================================
    27.             End If
    28.         Next i
    29.         ShellSort(strArray, lineCount)
    30.         tb1.Text = ""
    31.         curline = 1
    32.         For i = 1 To lineCount
    33.             tb1.Text = tb1.Text & strArray(curline) & vbCrLf
    34.             curline += 1
    35.         Next i
    36.         tb1.Select(1, 0)

    This I have in a module:
    VB Code:
    1. Sub ShellSort(ByRef sort() As String, ByVal numOfElements As Short)
    2.         Dim temp As String
    3.         Dim i, j, span As Short
    4.         span = numOfElements \ 2
    5.         Do While span > 0
    6.             For i = span To numOfElements - 1
    7.                 For j = (i - span + 1) To 1 Step -span
    8.                     If sort(j) <= sort(j + span) Then Exit For
    9.                     temp = sort(j)
    10.                     sort(j) = sort(j + span)
    11.                     sort(j + span) = temp
    12.                 Next j
    13.             Next i
    14.             span = span \ 2
    15.         Loop
    16.     End Sub
    I would really appreciate some help with this.
    Thanks!!!
    Last edited by Kramer3; Mar 13th, 2004 at 04:03 AM.

  2. #2
    New Member
    Join Date
    Mar 2004
    Posts
    6
    put a break on that line so when your code is executing it will pause on that line, then you can see what the value of curline is, what is happening is you have an array (strArray), and it has a certain length to it. if it's length is 2:
    strArray(0) & strArray(1) and you try to do this: strArray(2) = "something", then you will pull that same error that you are receiving.

    something you can add to that to stop getting that error (but sounds like you have a bug with the logic, I am having a slightly hard time following it) is the following:
    Instead of strArray(curline) = 1
    make it:
    if strArray.length <= curline then strArray(curline) = 1

    That will cause THAT exception to stop popping up, but probably create another bug somewhere else.

    Basically in a nutshell though, the value of curline is greater then the length of strArray, gotta stop that from happening.

  3. #3
    New Member
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1
    Kramer3 - he code you posted looks some how familiar.

    Your next question would be to sort the text in the array into columns of Type codes 1 - 6?

    I have been looking for a work around to it as the code is ambigous to look at as well.

    Mark
    Thanks for the Tip!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width