wuthless Posted October 25, 2006 Posted October 25, 2006 Hey All, I would like to count the number of times a word shows up in a text file. Say you have a text file that contains: Up, this works Down, this doesn't work Up, this works Up, this works Down, this doesn't work etc.. I could like to count the number of times 'Up' occurs and the number of times 'Down' occurs. I've tried several things like reading the file to an array and breaking out the 'Up' part but can't seem to get it total the number. It just says it's '1' and doesn't increment. Let me know what I'm doing wrong here Dim $v_sw_down = 0 Dim $v_sw_up = 0 _FileReadToArray($input_file, $s_swportlist) ;_ArrayDisplay($s_swportlist, "Switch Ports") ; Reads the switch port status For $x = 2 to UBound($s_swportlist) -2 $sw_portline = $s_swportlist[$x] $a_sw_portline = StringSplit($sw_portline," ", 1) ; Figures out switch state If StringInStr($a_sw_portline[2], "Down") Then $v_sw_down = ($v_sw_down + 1) EndIf MsgBox(0,"","Down Ports " & $v_sw_down & "/ Up Ports " & $v_sw_up)
litlmike Posted October 25, 2006 Posted October 25, 2006 I admit I am having a hard time with this one too. My best guess: If you can isolate the 'Up's and 'Down's to their own files/arrays, then it should be easy to count them. When using _FileReadToArray the 0 index for the array will tell you how many records there are. So, if you can _FileReadToArray just all the 'Up's and just all the 'Down's one at a time, then the 0 index should just be equal to what you are looking for. Example in Pseudo code: #include <File.au3> _FileReadToArray($sFilePathToJustTheUps, $aArrayUP) _FileReadToArray($sFilePathToJustTheDowns, $aArrayDOWN) Msg (4096, "UPs", "You have this many occurences of UP:" & $aArrayUP, 5) Msg (4096, "DOWNs", "You have this many occurences of DOWN:" & $aArrayDOWN, 5) Good Luck! _ArrayPermute()_ArrayUnique()Excel.au3 UDF
Rad Posted October 25, 2006 Posted October 25, 2006 $file = FileOpenDialog("Select your file",@DesktopDir,"Text document (*.txt)") $data = FileRead($File, 1024) $upscount = 0 $Downscount = 0 $lines = StringSplit($data, @lf) If IsArray($lines) Then $linecount = $lines[0] Else $linecount = 0 Endif msgbox(0,"File data",$linecount) For $i = 1 to $linecount If StringinStr(FileReadLine($file, $i), 'up') Then $upscount = $upscount + 1 EndIf If StringinStr(FileReadLine($file, $i), 'down') Then $downscount = $downscount + 1 EndIf Next msgbox(0,"File data",$data) msgbox(0,"Ups/Downs returned","Ups: " & $upscount & @LF & "Downs: " & $downscount) Its wierd, try to understand whats going on... I didnt but it worked LOL I used this txt file and it returned 5 up / 6 down: up down up up down up down down down left right up down
Valuater Posted October 25, 2006 Posted October 25, 2006 ************ not tested #include <File.au3> Dim $v_sw_down = 0 Dim $v_sw_up = 0 Dim $s_swportlist _FileReadToArray($input_file, $s_swportlist) ;_ArrayDisplay($s_swportlist, "Switch Ports") ; Reads the switch port status For $x = 1 To UBound($s_swportlist) - 1 If StringInStr($s_swportlist[$x], "Down") Then $v_sw_down = ($v_sw_down + 1) EndIf If StringInStr($s_swportlist[$x], "Up") Then $v_sw_up = ($v_sw_up + 1) EndIf Next MsgBox(0, "", "Down Ports " & $v_sw_down & "/ Up Ports " & $v_sw_up) 8)
Rad Posted October 25, 2006 Posted October 25, 2006 (edited) That works valuater Heres a little include: expandcollapse popup;=============================================================================== ; ; Description: Counts the number lines that contain a certain string in a file ; Syntax: _FileCountOccurrences($sFile, $sString) ; Parameter(s): $sFile - File to be examined ; $sString - String to be searched for ; Requirement(s): None ; Return Value(s): Success - Returns number of occurences ; Failure - Sets @error to 1 and returns 0 ; Author(s): Rad (RadleyGH at GMail dot com) ; Note(s): None ; ;=============================================================================== Func _FileCountOccurrences($sFile, $sString) Local $data, $stringcount, $lines, $linecount, $i $data = FileRead($sFile) $stringcount = 0 $lines = StringSplit($data, @lf) If IsArray($lines) Then $linecount = $lines[0] Else $linecount = 0 Endif For $i = 1 to $linecount If StringinStr(FileReadLine($sFile, $i), $sString) Then $stringcount = $stringcount + 1 EndIf Next If $StringCount > 0 Then Return $stringcount Else SetError(1) Return 0 EndIf EndFunc It only counts one per line, though (I Same for valuaters ;P) You could just add it to Misc.au3, or do what I do and put it in Rad.au3 (these go in your autoit\beta\include folder, also add #Include-Once to the top if your making a new file) Edited October 25, 2006 by Rad
Valuater Posted October 25, 2006 Posted October 25, 2006 rad... you didn't test that $data = FileRead($File, 1024)???? 8)
Rad Posted October 25, 2006 Posted October 25, 2006 (edited) Yeah I did and it worked fine, why? I just figure put a big number there since I dont see any FileGetTextSize() What should it be? ps. im a spammer lol Edited October 25, 2006 by Rad
Valuater Posted October 25, 2006 Posted October 25, 2006 _FileCountOccurrences($sFile,.... FileRead($File,... they dont match... also FileRead($sFile, FileGetSize($sFile)) ???? 8)
Rad Posted October 25, 2006 Posted October 25, 2006 (edited) You give the file with _FileCountOccurrences and then it reads it FileGetSize(): Returns the size of a file in bytes. Not the count Omg FileRead($file[, $count]) $count = [optional] The number of characters to read. Default read the entire file. Just remove the count completely Edited October 25, 2006 by Rad
Valuater Posted October 25, 2006 Posted October 25, 2006 rad... study the first two lines of my last post 8)
Rad Posted October 25, 2006 Posted October 25, 2006 Oh I get it ^^ I was using $File = FileOpenDialog in the test script, which FileRead($File) used that, it didnt convert to $sFile, so it still worked updated
Moderators SmOke_N Posted October 25, 2006 Moderators Posted October 25, 2006 (edited) Func _FileCountOccurrences($sFile, $sString, $vCase = 0) If FileExists($sFile) Then $sFile = FileRead($sFile) StringReplace($sFile, $sString, '', $vCase) Return @extended EndFunc Edit: Forgot a parameter. Edited October 25, 2006 by SmOke_N 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.
Valuater Posted October 25, 2006 Posted October 25, 2006 Common sense plays a roll in the basics of understanding AutoIt...well... maybe someday i will have thatlol8)
Moderators SmOke_N Posted October 25, 2006 Moderators Posted October 25, 2006 well... maybe someday i will have thatlol8)Nah, sometimes we just all just get caught up in one way of doing it. Gary and Jdeb are good at doing that to me! 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.
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