Sign in to follow this  
Followers 0
litlmike

Counting Unique Strings

6 posts in this topic

#1 ·  Posted (edited)

Problem:

Need to read a file of States, count the total of times that State appears, then generate a report of how many times each state appears.

Edit: We do not know how many different state names will appear (i.e likely over 50).

Example:

5 states in the File:

CA

CA

CA

MT

VT

Run script should Report back:

CA 3

MT 1

VT 1

I don't have much for code yet, I am stumped where to go from here.

Thanks in advance.

#include <File.au3>
#include <Array.au3>

Dim $aArray
$sFilePath = @ScriptDir & "\VIRs.txt"

_FileReadToArray ( $sFilePath, $aArray )
_ArrayDisplay ($aArray, "")

$counter = 0

While $counter < UBound ($aArray)
    
    $counter +=1
WEnd
Edited by litlmike

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Something like this?

Dim $aArray
$aCount = _ArrayCreate("void", 0, 0, 0)
$aDefault = _ArrayCreate("3", "CA", "MT", "VT")
$sFilePath = @ScriptDir & "\VIRs.txt"

_FileReadToArray ( $sFilePath, $aArray )

For $i = 1 To (UBound($aArray) - 1)
    For $j = 1 to (UBound($aDefault) - 1)
        If $aArray[$i] = $aDefault[$j] Then $aCount[$j] += 1
    Next
Next
    
MsgBox(0, "States found", "CA: " & $aCount[1] & @CRLF & "MT: " & $aCount[2] & @CRLF & "VT: " & $aCount[3])

Edit: Indexing typo.

Edited by mikehunt114

IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]

Share this post


Link to post
Share on other sites

StringReplace + @extended may be easier if it's just states in the file and now chance of something else having the same name:

Global $sRead = 'CA CA CA MT VA MT';FileRead('FileNameLocation.txt')
Global $aStates = StringSplit('CA,MT,VA', ',')
Global $sHold = '', $nExt = ''
For $iCC = 1 To Ubound($aStates) - 1
    StringReplace($sRead, $aStates[$iCC], '')
    $nExt = @extended
    If $nExt Then $sHold &= $aStates[$iCC] & ' ' & $nExt & @CRLF
Next
$sHold = StringTrimRight($sHold, 2)
MsgBox(64, 'Info', $sHold)


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Okay I got closer to an answer. I don't quite understand a few things, so I can't take it to completion.

1) The purpose of this? Global $sHold = '', $nExt = ''

2) How to NOT create a duplicate in the output (If CA appears 3 times, msgbox shows 'CA 3' 3 times.

Dim $aStates
Global $sFilePath = @ScriptDir & "\VIRs.txt" ;FileRead('FileNameLocation.txt')
_FileReadToArray ( $sFilePath, $aStates )
$sRead = _ArrayToString ($aStates,@CR)
Global $sHold = '', $nExt = ''

For $iCC = 1 To Ubound($aStates) - 1
    StringReplace($sRead, $aStates[$iCC], '')
    $nExt = @extended
    If $nExt Then $sHold &= $aStates[$iCC] & ' ' & $nExt & @CRLF
Next
$sHold = StringTrimRight($sHold, 2)
MsgBox(0, 'Info', $sHold)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I got it! I figured out that the '' was to delete all the occurences of a string. What the code needed was to redefine what the new string was! Solution below.

Dim $aStates
Global $sFilePath = @ScriptDir & "\VIRs.txt" ;FileRead('FileNameLocation.txt')
_FileReadToArray ( $sFilePath, $aStates )
$sRead = _ArrayToString ($aStates,@CR)
Global $sHold = '', $nExt = ''

For $iCC = 1 To Ubound($aStates) - 1
    $StringReplace = StringReplace($sRead, $aStates[$iCC], '')
    $nExt = @extended
    If $nExt Then
        $sHold &= $aStates[$iCC] & ' ' & $nExt & @CRLF
        $sRead = $StringReplace
    EndIf
Next
$sHold = StringTrimRight($sHold, 2)
MsgBox(64, 'Info', $sHold)
Edited by litlmike

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

And my way. :P

edit - Sorry, I was only answering this part "How to NOT create a duplicate in the output"

Local $temp, $result

Dim $myarray[5] = ['CA', 'CA', 'CA', 'MT', 'VT']

For $i = 1 To UBound($myarray) - 1
    If Not StringInStr($temp, $myarray[$i]) Then $result &= $myarray[$i] & @CR
    $temp &= $myarray[$i] & ' '
Next

MsgBox(0, '', $result)

edit - removed unnecessary udfs.

Edited by xcal

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0