[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help with vbs

Posted on 2011-09-03
3
Medium Priority
?
385 Views
Last Modified: 2012-05-12
I got a text file that looks like
aaa
bbb
ccc
aaa

Is it possible to have vbs to output the number of counts per word and then display the word that has the highest number of counts?
aaa = 2
bbb = 1
ccc = 1

The highest (number of counts) = aaa
0
Comment
Question by:kt2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 2000 total points
ID: 36478003
Try the following code. Set the input filepath.

Incase there are multiple with same highest count, it will display all those words.

regards
Prashanth

input_file="C:\input.txt"

Set re = CreateObject("VBScript.RegExp")
Set oFileObj = CreateObject("Scripting.FileSystemObject")
Set oTxtStream = oFileObj.OpenTextFile(input_file, 1)

logstr = oTxtStream.ReadAll
oTxtStream.Close
Set oTxtStream = oFileObj.OpenTextFile(input_file, 1)

strarr=" "
strhigh=""
ctr=0
Do While Not oTxtStream.AtEndOfStream
    strline=oTxtStream.ReadLine
    If InStr(strarr," "& strline&" ")=0 Then 
    	strarr=strarr & strline&" "
        re.Pattern = Trim(strline)
        re.IgnoreCase = True
        re.Global = True
        Set ma = re.Execute(logstr)
        mcount=ma.Count
        'WScript.echo  strline & mCount
        If mcount > ctr Then
        	ctr=mcount
        	strhigh=strline
        ElseIf mcount = ctr Then
        	strhigh=strhigh & " and " & strline
        End if
        
    End If
    
Loop
WScript.echo "The highest (number of counts) = " & strhigh & " - Count = " &ctr

Open in new window

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 36478278
kt2003,

Depending on what you mean by "counts per word", the code you accepted might not be delivering what you want.

For example, suppose your input file contains:

aaa
bbb
ccc
aaa
bbbbbb

The code you accepted will say that 'bbb' is the most frequently occurring word, with a count of three.  I doubt that that was what you wanted.

You also indicated in your question that you wanted to output the counts for all "words".  The code you accepted does not do that.

Patrick
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 36478411
Thanks Patrick...

@kt2003 you can try the modified script.
input_file="C:\input.txt"

Set oFileObj = CreateObject("Scripting.FileSystemObject")
Set oTxtStream = oFileObj.OpenTextFile(input_file, 1)

strarr=" "
strhigh=""
ctr=0
Do While Not oTxtStream.AtEndOfStream
    strline=oTxtStream.ReadLine
    If InStr(strarr," "& strline&" ")=0 Then 
        strarr=strarr & strline&" "
        
        Set oTxtStream1 = oFileObj.OpenTextFile(input_file, 1)
        mcount=0
        Do While Not oTxtStream1.AtEndOfStream
            strline1=oTxtStream1.ReadLine
            If LCase(strline)=LCase(strline1) Then 
                mcount=mcount+1
            End If
            
        Loop
        oTxtStream1.close
        WScript.echo  strline & " - " & mCount
        If mcount > ctr Then
            ctr=mcount
            strhigh=strline
        ElseIf mcount = ctr Then
            strhigh=strhigh & " and " & strline
        End If
    End If    
Loop

WScript.echo "The highest (number of counts) = " & strhigh & " - Count = " &ctr

oTxtStream.close

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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