Results 1 to 24 of 24

Thread: Sorting a large text file ..... waves to Hack :)

  1. #1

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Resolved Sorting a large text file ..... waves to Hack :)

    Based on the other thread with the same name which is marked as reolved ...

    I'm now using this Quicksort routine to sort a text file thats nearly 2 meg in size.
    It contains fixed length records.
    I towkred before, but now the file is bigger I am getting "Input Past End Of File" error messages on the line of code highlighted below.
    Anyone know why ?

    VB Code:
    1. ' Now sort file into alphabetical order
    2.     Open "C:\att_value.txt" For Input As #1
    3.      [B]str1 = Split(Input$(LOF(1), 1), vbCrLf)[/B]
    4.     Close #1
    5.    
    6.     QuickSort str1, LBound(str1), UBound(str1)
    7.    
    8.     Open "C:\att_value.txt" For Output As #1
    9.      Print #1, Join(str1, vbCrLf)
    10.     Close #1
    Last edited by TheBionicOrange; Jan 25th, 2007 at 05:01 AM.

  2. #2

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file

    Its OK I found out why.
    I had a blank line at the end of the file ! Doh !!!

  3. #3

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file

    More annoyingly ... I still get the problem ... and its starting to drive me mad !!!

    Why does the Split command give me end of file errors ?!!?!

    If I edit the file, press backspace to remove the offending line, save it, and then run my code .. it works fine.
    If I then edit the file, go the end and press carriage return, in affect putting the blank line back, save it, then run my code ... it works ?!?!!??!?!?!?!?!!!

    Can someone help .... I'm fast running out of ideas here

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sorting a large text file ..... waves to Hack :)

    It sounds like your sort routine is putting an EOF marker in your file.

    Does your sort routine check for BOF and EOF?

  5. #5

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    I'm using these 2 routines :

    VB Code:
    1. Private Sub QuickSort(C() As String, ByVal First As Long, ByVal Last As Long)
    2.     Dim Low As Long, High As Long
    3.     Dim MidValue As String
    4.    
    5.     Low = First
    6.     High = Last
    7.     MidValue = C((First + Last) \ 2)
    8.    
    9.     Do
    10.         While C(Low) < MidValue
    11.             Low = Low + 1
    12.         Wend
    13.        
    14.         While C(High) > MidValue
    15.             High = High - 1
    16.         Wend
    17.        
    18.         If Low <= High Then
    19.             Swap C(Low), C(High)
    20.             Low = Low + 1
    21.             High = High - 1
    22.         End If
    23.     Loop While Low <= High
    24.    
    25.     If First < High Then QuickSort C, First, High
    26.     If Low < Last Then QuickSort C, Low, Last
    27. End Sub
    28.  
    29. Private Sub Swap(ByRef A As String, ByRef B As String)
    30.     Dim T As String
    31.    
    32.     T = A
    33.     A = B
    34.     B = T
    35.    
    36. End Sub

    And then sorting the fields thus :

    VB Code:
    1. ' Now sort file into alphabetical order
    2.     Open "C:\att_value.txt" For Input As #1
    3.        
    4.     str1 = Split(Input$(LOF(1), 1), vbCrLf)
    5.     Close #1
    6.    
    7.     QuickSort str1, LBound(str1), UBound(str1)
    8.    
    9.     Open "C:\att_value.txt" For Output As #1
    10.      Print #1, Join(str1, vbCrLf)
    11.     Close #1

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Sorting a large text file ..... waves to Hack :)

    Join(str1, vbCrLf) is putting vbCrLf at the right end of the string. If you don't want that blank line, join to a string variable, delete the last 2 characters (take Right$() the length of the string - 2) and Print the string variable.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    Its not getting as far sa the "Join" statement. Its falling over on the "Split" statement, with "Input past end of file".

  8. #8

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    Alternatively, as the offending line is always the last line, is there a quick way of opening a file, and then doing the equivalent of a <CTRL/END> and a <BACKSPACE>, which would in effect delete the line ?

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Sorting a large text file ..... waves to Hack :)

    that error usually occurs when the file has null characters in it. Try loading the file by opening it for Binary and placing it in a buffer - see the file threads in the FAQ for a code sample.

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Sorting a large text file ..... waves to Hack :)

    Or fix the file in Notepad - backspace out the last, blank, line. Then the program should work from then on. The problem is that, when the program DOES get past the Join line, and saves the sorted file, it outputs that last blank line. From then on, you can't get past the Input function.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  11. #11
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Sorting a large text file ..... waves to Hack :)

    Quote Originally Posted by Al42
    Join(str1, vbCrLf) is putting vbCrLf at the right end of the string.
    The Join() function is not putting vbCrLf at the end of the string. The Print statement is adding vbCrLf to the file after outputting the joined string. To avoid this, use a semicolon at the end of the statement.

    VB Code:
    1. Print #1, Join(str1, vbCrLf);

  12. #12

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    Bushmobile I think you solved it !
    Opening the file in binary before performing the split certainly stops an "end of file" error being thrown up, and the sort works fine.
    The only "slight niggle" I am left with is that I now have a blank line at the top of the file rather than at the bottom, no doubt due to the sort.
    Not sure if this is going to annoy the import routines that pick this file up, but I can't try them out until later.
    Logophobic, I tried your method of suffixing a semicolon to try and prevent the blank line appearing, but that didn't seem to make any difference.

    Anyway ... cheers guys. At least I'm not crashing now
    Last edited by TheBionicOrange; Jan 25th, 2007 at 04:07 AM.

  13. #13
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Sorting a large text file ..... waves to Hack :)

    Hey TheBionicOrange, Happy New Year

    As the result is in an Array (I think) the can you loop thru the Array and only output (Print to file) anything other than a Blank line?

  14. #14

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    Hey Bruce happy new year to you too !
    As for your second sentence ... ummm .... eh ? ha ha
    If it helps .. I don't actually print any blank lines to my file. There just seemed to be one at the end when I edited it, although strangely its not shown if I throw the file into MS Word, only notepad or wordpad ?! I presume Word must strip out the null (or whatever it is) at the end automatically.

  15. #15
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Sorting a large text file ..... waves to Hack :)

    $10 its a 'spare' Carriage return (during the Join).

    To test, try removing the last Array element - ReDim Preserve str1(Ubound(str1) - 1)

  16. #16

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    You owe me $10 ... although I would rather have it in pounds
    The "blank line" is in the file BEFORE I even get to the join statement.
    Its in there before I even get as far as the SPLIT statement.

  17. #17
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Sorting a large text file ..... waves to Hack :)

    Why do you think I mentione dollars - and Aussie Dollars too. That equates to 5 pound

    If the blank line is in the base document, then (noting your Inputing as Binary) can you loop thru the Array and remove it prior to the Quick sort?

  18. #18

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    Trying that now ..... (takes about 15 minutes to extract).

    Watch this space ...

  19. #19

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    Bu**er ! Looks like I need to take a hammer to my piggy bank !
    You were quite right.

    ReDim Preserve str1(Ubound(str1) - 1)

    .. did indeed remove the blank line.

    Bruce ... thank you very much for the effort. Also to everyone else who contributed to this thread.
    You have no idea how annoyed I was at the flaming line yesterday !

    p.s. I did try and add to your rating, but apparently I have to "spread it around a bit" first. Personally I think thats a pretty stupid rule !

  20. #20
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Sorting a large text file ..... waves to Hack :)

    No problem. Good to see you back on the board.

    However, will that 'extra' vbCrLf always be pressent? In any case you could check for the blank line (in the last element) before ReDimming the Array.

  21. #21

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    Yes it almost inevitably will be, but putting the check in wouldn't hurt.

    Thanks again for your help ... top man


    Steve.

  22. #22
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Sorting a large text file ..... waves to Hack :)

    I have to ask. Do you want to improve speed at all (is 15 minutes acceptable)?

  23. #23

    Thread Starter
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: Sorting a large text file ..... waves to Hack :)

    No .. 15 minutes in my book is NOT acceptable. Unfortunately, due to the crappy system we have here, thats about as fast as its going to get.
    Its an ALPHA box running VMS. I'm tapping into it via middleware called "Attunity", which is not the most astounding piece of software I have ever had to deal with.
    We are in a transitional period. One day (probably when I am retired) we will eventually be on Oracle, or at least something thats not made of wood !
    15 minutes is only at this time due to the number of users on the system. If I ran it at 5pm its about 5 minutes. our Operations Department will be running it on the weekend, where that time will go down to about 2-3 minutes, which is much more acceptable.

  24. #24
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Sorting a large text file ..... waves to Hack :)

    Ah, so you use an abacus too.

    Anyhoo, all the best.

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