?
Solved

Sort Program-How to?

Posted on 2006-05-30
8
Medium Priority
?
230 Views
Last Modified: 2010-04-07
I have more than 3 words I am trying to sort. (I am self learning programming and they say the sort is the best place to start)

Zebra
Transportation
And

My code is below. I am able to declare my variables, open a text file with the above words in it and write those words to another file. My problem lies in sorting them. The looping and if statement are giving me problems. Help! When I run the .vbs program I get an error that says line 19 char 5, input past end of file. I tryed doing something with (x + 1) but I am failing because of my lack of knowledge. Thankyou for helping!!

'=============================Step 1
Const ForReading = 1
Const ForWriting = 2
Dim SW
Dim Word1
Dim Word2
SW=0
Word1=0
Word2=0

'=============================Step 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForReading)

'=============================Step 3    
do until objFile.AtEndOfStream
    Word1 = objFile.Readline
       Wscript.Echo word1
    word2 = objFile.Readline
       Wscript.Echo word2
    If word1 > word2 then
    word2 = replace(word2, "word1", "word2")
    sw=1
    end if  
loop    

Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test2.txt", ForWriting)    
    objFile.Write word2
    objfile.writeline
    objFile.Write word1
    objFile.Close  
0
Comment
Question by:tsi_admin
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16794257
Here is an implementation that uses QuickSort:

Option Explicit

Dim fileName
fileName = "C:\Documents and Settings\Michael\My Documents\1 VB Code\2 VBScript Testing\Test.txt"

Dim fso, f
Const ForReading = 1, ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fileName) Then
   Set f = fso.OpenTextFile(fileName, ForReading, False)
   Dim lines ' convert file lines to an array after trimming trailing CrLfs
   lines = f.ReadAll
   f.Close
   While Right(lines, 2) = vbCrLf
       lines = Left(lines, Len(lines) - 2)
   Wend
   lines = Split(lines, vbCrLf)    

   ' sort the array using Quicksort
   QuickSort lines, LBound(lines), UBound(lines)

   ' Put array back together as one string
   lines = Join(lines, vbCrLf) & vbCrLf

   ' Overwrite the existing File with the sorted data
   Set f = fso.OpenTextFile(fileName, ForWriting, True)
   f.Write(lines)  
   f.Close

   MsgBox fileName, vbOKOnly, "Lines Have Been Sorted"
Else
   MsgBox fileName, vbOKOnly, "File Not Found"
End If



Private Sub Quicksort(ByRef list, ByVal min, ByVal max)
    Dim med_value
    Dim hi
    Dim lo
    Dim i

    ' If the list has no more than 1 element, it's sorted.
    If min >= max Then Exit Sub

    ' Pick a dividing item.
    i = Int((max - min + 1) * Rnd + min)
    med_value = list(i)

    ' Swap it to the front so we can find it easily.
    list(i) = list(min)

    ' Move the items smaller than this into the left
    ' half of the list. Move the others into the right.
    lo = min
    hi = max
    Do
        ' Look down from hi for a value < med_value.
        Do While list(hi) >= med_value
            hi = hi - 1
            If hi <= lo Then Exit Do
        Loop
        If hi <= lo Then
            list(lo) = med_value
            Exit Do
        End If

        ' Swap the lo and hi values.
        list(lo) = list(hi)
       
        ' Look up from lo for a value >= med_value.
        lo = lo + 1
        Do While list(lo) < med_value
            lo = lo + 1
            If lo >= hi Then Exit Do
        Loop
        If lo >= hi Then
            lo = hi
            list(hi) = med_value
            Exit Do
        End If

        ' Swap the lo and hi values.
        list(hi) = list(lo)
    Loop

    ' Sort the two sublists
    Quicksort list, min, lo - 1
    Quicksort list, lo + 1, max
End Sub
0
 

Author Comment

by:tsi_admin
ID: 17042473
This is what I came up with for a final answer.

Const ForReading = 1
Const ForWriting = 2
Dim SW
Dim Word1
Dim Word2
Dim Word3 'Tempholder variable
SW=1

Do until SW=0  
SW=0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForReading)
Set objtest = objFSO.OpenTextFile("D:\Temp\sort\test2.txt", ForWriting)
Set objcopy = objfso.getfile ("D:\Temp\sort\test2.txt")

     Word1 = objFile.Readline
        'Wscript.Echo word1
  Do Until objFile.AtEndOfStream 'Check to see if EOF
     word2 = objFile.Readline
        'Wscript.Echo word2
     If word1 > word2 then 'Swaps alpebetical order of words
        word3=word1
        word1=word2
        word2=word3
        SW=1
     end if
     'If word1 = word2 then
       ' objtest.writeline
     'End if
    objtest.writeline (word1)
    Word1 = Word2
  loop
objtest.writeline (word2)
objfile.Close
objtest.Close
Objcopy.copy("D:\Temp\sort\test.txt")
Loop
Wscript.Echo "Completed sort"
Wscript.quit
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17042658
Did you not try my solution?...

It will work with ANY number of entries in your file.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:tsi_admin
ID: 17042673
I did try it, thanks for the input, however, based upon my requirements for the project I had to go a different route.

Thanks to all for your input.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17042683
Good luck then... =)
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 17100919
PAQed with points refunded (325)

DarthMod
Community Support Moderator
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question