• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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
0
a23m2000
Asked:
a23m2000
1 Solution
 
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.
0
 
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
0
 
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  
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Explain in word what are you trying to do first   8->
0
 
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.
0
 
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

0
 
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
0
 
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
0
 
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?
0
 
EDDYKTCommented:
>>just single word in a single row in a text file.

Inb this case, you delimiter is vbcrlf
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now