Jump to content

Count the number of occurrences in a text file / array


 Share

Recommended Posts

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)
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

$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
Link to comment
Share on other sites

************ 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)

NEWHeader1.png

Link to comment
Share on other sites

That works valuater

:whistle:

Heres a little include:

;===============================================================================
;
; 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 by Rad
Link to comment
Share on other sites

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 :whistle:

Edited by Rad
Link to comment
Share on other sites

  • Moderators

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 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.

Link to comment
Share on other sites

  • Moderators

well... maybe someday i will have that

lol

8)

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! :whistle:

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...