VBScript - Easy Points.

I am learning programming from some books and at work. It is a sort program (bubble sort) that takes a text file and sorts the words Alpehbetically . I got the first two (2) words to sort, but anymore than 2 it bombs out. My programming knowledge could use some help. Any ideas? Here is my code so far.

Const ForReading = 1
Const ForWriting = 2
Dim SW '(switch word)
Dim Word1
Dim Word2

SW=0
Word1=0
Word2=0

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

do until objfile.atendofstream
    word1 = objFile.Readline
        Wscript.Echo word1

    word2 = objFile.Readline
        Wscript.Echo word2

    If word1 > word2 then
    objFile.Close
        word2 = replace(word2, "word1", "word2")
    Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForWriting)
    objFile.Write word2
    objfile.writeline
    objFile.Write word1
    objFile.Close
   
    end if
wscript.echo "Got milk"
loop  
        sw=0

Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForWriting)
    objFile.Write word2
    objfile.writeline
    objFile.Write word1
objFile.Close
a23m2000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ronkleinCommented:
this is the "easy" solution that comes to my mind:
read the whole file into an array, each word is an item in that array.
sort the array.
dump the array to an output file.
BrianGEFF719Commented:
explain what you are trying to do with:
  If word1 > word2 then

Are word1 and Word2 Numbers? Or do you wish to compare their ascii values?


Secondly what is with this:
word2 = replace(word2, "word1","word2") <---- if the string "word1" is contained in the variable word2, then replace it with the string "word2", is this what you want?

>>
    objFile.Write word2
    objfile.writeline
    objFile.Write word1


I think it should be:

    objFile.writeline word2
    objFile.writeline word1



-Brian
EDDYKTCommented:
don't know what are you trying to do


do until objfile.atendofstream  ' will fail because you already close all file handle
    word1 = objFile.Readline
        Wscript.Echo word1

    word2 = objFile.Readline
        Wscript.Echo word2

    If word1 > word2 then
    objFile.Close
        word2 = replace(word2, "word1", "word2")
' Inside the loop try to write to same file?
    Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForWriting)
    objFile.Write word2
    objfile.writeline
    objFile.Write word1
    objFile.Close
   
    end if
wscript.echo "Got milk"
loop  
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

EDDYKTCommented:
by the way, what is test.txt looks like?

Explain in word what are you trying to do first   8->
a23m2000Author Commented:
Thanks for all of your responses. This is my goal.

File: test.txt
Contents of text.txt file:

         the
         and
         whatever
         it

Goal: Launch the program (in this case a script written in VBScript)Sort the text.txt file alphebetically.

Notes:

"If word1 > word2 then" - 'What I am trying to do here is take word1 (the) and word2 (and), compare them and determine which is larger alphbetically and write them back to the file.

--------------------------------------------------------------------
this is the "easy" solution that comes to my mind:
read the whole file into an array, each word is an item in that array.
sort the array.
--------------------------------------------------------------------
ronklein - Can you provide some sample code to accomplish the above?

Thanks
A.M.
dump the array to an output file.
EDDYKTCommented:
Try this

Private Const ForReading = 1
Private Const ForWriting = 2

   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForReading)
    Word1 = objFile.ReadAll
    objFile.Close
    arr = Split(Word1, vbCrLf)
    Quicksort arr, LBound(arr), UBound(arr)
    Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForWriting, true)
    objFile.Write Join(arr, vbCrLf)
    objFile.Close

Sub Quicksort(List, min, max)

    ' 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
a23m2000Author Commented:
EDDYKT, That worked, however my questions are below. (I am trying to understand how it is working to improve my programming skills.)

Private Const ForReading = 1 '(What is the difference between private const and const?)
Private Const ForWriting = 2

   
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForReading)
    Word1 = objFile.ReadAll
    objFile.Close

    arr = Split(Word1, vbCrLf)                             '(What does this do?)
    Quicksort arr, LBound(arr), UBound(arr)          '(What does this do?)

    Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForWriting, true)
    objFile.Write Join(arr, vbCrLf)                         '(What does this do?)
    objFile.Close

Sub Quicksort(List, min, max)                              '(What does all the below do , I understand this is a subroutine?)

    ' 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
EDDYKTCommented:
Word1 = objFile.ReadAll    'read all contents
    objFile.Close

    arr = Split(Word1, vbCrLf)                             'split the line into array (break vbcrlf)
    Quicksort arr, LBound(arr), UBound(arr)          ' call the sort routine
    Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForWriting, true)
    objFile.Write Join(arr, vbCrLf)                         ' join the array back to one string with vbcrlf delimiter and write back to file
    objFile.Close

Sub Quicksort(List, min, max)                              ' this is quick sort routine
a23m2000Author Commented:
Eddykt,

Do I need to use the split function if it only needs to be in one array, in other words there is no delimited string, just single word in a single row in a text file. Also can in your script can we get rid of some of the i = because of the single row?
EDDYKTCommented:
>>just single word in a single row in a text file.

Inb this case, you delimiter is vbcrlf
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.