# Counting Unique Strings

## Recommended Posts

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.

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

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

_ArrayDisplay (\$aArray, "")

\$counter = 0

While \$counter < UBound (\$aArray)

\$counter +=1
WEnd```
Edited by litlmike
##### Share on other sites

Something like this?

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

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 on other sites
• Moderators

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
\$nExt = @extended
If \$nExt Then \$sHold &= \$aStates[\$iCC] & ' ' & \$nExt & @CRLF
Next
\$sHold = StringTrimRight(\$sHold, 2)
MsgBox(64, 'Info', \$sHold)```

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.

##### 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')
Global \$sHold = '', \$nExt = ''

For \$iCC = 1 To Ubound(\$aStates) - 1
\$nExt = @extended
If \$nExt Then \$sHold &= \$aStates[\$iCC] & ' ' & \$nExt & @CRLF
Next
\$sHold = StringTrimRight(\$sHold, 2)
MsgBox(0, 'Info', \$sHold)```
##### Share on other sites

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')
Global \$sHold = '', \$nExt = ''

For \$iCC = 1 To Ubound(\$aStates) - 1
\$nExt = @extended
If \$nExt Then
\$sHold &= \$aStates[\$iCC] & ' ' & \$nExt & @CRLF
EndIf
Next
\$sHold = StringTrimRight(\$sHold, 2)
MsgBox(64, 'Info', \$sHold)```
Edited by litlmike
##### Share on other sites

And my way.

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

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...