litlmike Posted December 20, 2006 Posted December 20, 2006 (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:CACACAMTVTRun script should Report back:CA 3MT 1VT 1I 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 December 20, 2006 by litlmike _ArrayPermute()_ArrayUnique()Excel.au3 UDF
mikehunt114 Posted December 20, 2006 Posted December 20, 2006 (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 December 20, 2006 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]
Moderators SmOke_N Posted December 20, 2006 Moderators Posted December 20, 2006 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) 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.
litlmike Posted December 21, 2006 Author Posted December 21, 2006 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) _ArrayPermute()_ArrayUnique()Excel.au3 UDF
litlmike Posted December 21, 2006 Author Posted December 21, 2006 (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 December 21, 2006 by litlmike _ArrayPermute()_ArrayUnique()Excel.au3 UDF
xcal Posted December 21, 2006 Posted December 21, 2006 (edited) 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 December 21, 2006 by xcal How To Ask Questions The Smart Way
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now