[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript - Easy Points.

Posted on 2006-05-15
10
Medium Priority
?
187 Views
Last Modified: 2010-04-07
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
Comment
Question by:a23m2000
10 Comments
 
LVL 1

Expert Comment

by:ronklein
ID: 16686873
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
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16688558
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 16689670
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 16689674
by the way, what is test.txt looks like?

Explain in word what are you trying to do first   8->
0
 

Author Comment

by:a23m2000
ID: 16690765
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
 
LVL 26

Accepted Solution

by:
EDDYKT earned 1260 total points
ID: 16691242
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
 

Author Comment

by:a23m2000
ID: 16693275
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 16693715
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
 

Author Comment

by:a23m2000
ID: 16711460
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 16711506
>>just single word in a single row in a text file.

Inb this case, you delimiter is vbcrlf
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

873 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