hogfan

Help with Infinite Loop!

5 posts in this topic

#1 ·  Posted

Hello,

I have put together a new script that I want to use to monitor changes to the clipboard to look for text copied to the clipboard that has more than 2 x "/" characters in the string.  I also have the script populating an array from a text file stored in the same directory as the script, and want to use that to provide a list of exclusions to the rule mentioned above.  I am having an issue here though.  The script keeps getting into an infinite loop when I call my function to loop through the array of exclusion strings and I can't figure out why.  Below is the full code from my script.    The only external dependency is the "excludes.txt" file that will exist in the script directory  contains the following:

http://
https://
ftp://
www.
chrome://
.net
.com
.org

Basically, the goal of the script is to monitor the clipboard data, and when it changes, check if the string on the clipboard contains more than 2 "/" characters, and if so re-format it for a wildcard type search, EXCEPT WHEN the string contains one the exclusions in the excludes.txt file. (ie. URLs).  I'm sure I'm missing something simple.  Any help is appreciated.

#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <file.au3>

;Define the global variables
Global $myCircuit, $myWildCardCircuit, $Paused=False, $excludes, $arrExcludes, $origHWND,$lastCopied='',$WM_CLIPUPDATE=0x031D,$DefMsG='__•¯¯'

;set the hotkey used to toggle the script on/off
HotKeySet("{PAUSE}", "TogglePause")

While 1
;while the app is running, watch for changes to the clipboard and take appropriate formatting action
_OnClipBoardChange()
Sleep(1000)
WEnd

;###################################################################
;Function to toggle the script pause
;###################################################################
Func TogglePause()
$Paused = Not $Paused
While $Paused = True
  Sleep(100)
WEnd
EndFunc


;###################################################################
;Function to count occurences of a character in a string
;###################################################################
Func _CountStr($sStr, $sFind)
    StringReplace($sStr, $sFind, $sFind)
    Return @Extended
EndFunc


;#################################################################################
;Function to populate the list of strings to exclude from LIKE % search formatting
;#################################################################################
Func _PopulateExcludes()

_FileReadToArray(@ScriptDir & "\excludes.txt", $arrExcludes)

EndFunc

;#####################################################################################################
;Function to loop through the array of exclusions & check if any of the exclusions exist in our string
;#####################################################################################################
Func _CheckExcludes()
    ;clear previous value for $excludes
    $excludes=0

    ; check the string against a list of exclusions (ie. URLs)
    For $i = 1 to Ubound($arrExcludes)-1
        $excludes = StringInStr($myCircuit, $i,0)

        ;if exclusion found, then stop checking
        If $excludes > 1 Then
            ;Msgbox ("","Exclusion Found At",$excludes)

        EndIf
    Next
EndFunc



 ;#################################################################################
;function to reduce RAM usage due to loops
;#################################################################################
 Func _ReduceMemory($i_PID = -1)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf

    Return $ai_Return[0]
EndFunc



;#####################################################################################################
;Main function to monitor changes to the clipboard data and perform the appropriate formatting
;#####################################################################################################

Func _OnClipBoardChange()

    ;store clipboard contents as variable
   $myCircuit = ClipGet()

      If $myCircuit = $myWildCardCircuit Then
         Return ; No need to format the circuit because we just did

      Else

         Select

            Case $myCircuit > 2  ;clipboard is inaccessible
               _ReduceMemory()
               Return

            Case $myCircuit = 2 ;clipboard contains non-text data
               ; Do nothing as there is nothing to format
                _ReduceMemory()
               Return

            Case $myCircuit = 1 ;clipboard is empty
               ; Do nothing as there is nothing to format
                _ReduceMemory()
               Return

            Case Else  ;clipboard has valid data and we need to check it out
               ; check if the clipboard data is a circuit ID by counting the number of / characters in the string
               $slashCount = _CountStr($myCircuit, "/")

               ;call function to populate/refresh the exclusions array from "excludes.txt" in the script directory
                _PopulateExcludes()

               ;call function to check for exclusions
                _CheckExcludes()
                
                ;if clipboard data contains more than 2 slashes AND no exclusions were found
               If $slashCount > 2 And $excludes = 0 Then
                  ;replace the "/" with "%"
                  $myWildCardCircuit = StringReplace($myCircuit, "/", "%")
                  ;add the LIKE % and remove any white spaces
                  $myWildCardCircuit = "LIKE %" & StringStripWS($myWildCardCircuit,8) & "%"
                  ;send the formatted string to the clipboard
                  ClipPut($myWildCardCircuit)
                   _ReduceMemory()

               Else ; Do nothing because the string contains an exclusion and we don't want to format it for wildcard search
                   _ReduceMemory()
               EndIf
            EndSelect
      EndIf
EndFunc

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello hogfan,

It appears you are missing the actual array call in your StringInString() function.

Hope this helps you.

Func _CheckExcludes()
    ;clear previous value for $excludes
    $excludes=0

    ; check the string against a list of exclusions (ie. URLs)
    For $i = 1 to Ubound($arrExcludes)-1
        $excludes = StringInStr($myCircuit, $arrExcludes[$i])

        ;if exclusion found, then stop checking
        If $excludes Then
            ;Msgbox ("","Exclusion Found At",$excludes)

        EndIf
    Next
EndFunc

 

Realm

 

Edit: removed unintentional message.

Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thanks for that!  I have corrected that, but I still appear to be stuck in a loop.   

Share this post


Link to post
Share on other sites

#4 ·  Posted

I believe I have sorted out the looping issue.  Look like for some reason $excludes variable always =  0 or I have some code issue with the below if statement, because even strings that should be excluded are getting reformated...........

 

;if clipboard data contains more than 2 slashes AND no exclusions were found
               If $slashCount > 2 And $excludes = 0 Then
                  ;replace the "/" with "%"
                  $myWildCardCircuit = StringReplace($myCircuit, "/", "%")
                  ;add the LIKE % and remove any white spaces
                  $myWildCardCircuit = "LIKE %" & StringStripWS($myWildCardCircuit,8) & "%"
                  ;send the formatted string to the clipboard
                  ClipPut($myWildCardCircuit)
                   _ReduceMemory()

               Else 
               ; Do nothing because the string contains an exclusion and we don't want to format it for wildcard search
                   _ReduceMemory()
               EndIf

 

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I just noticed in your comment in Function _CheckExcludes() that if an exclusion is found it's supposed to stop checking. However I don't see anything to stop checking, so more than likely $excludes=0 is reset and the last element in your exclusions is passing a string that you once found an exclusion. Adding an Exitloop will stop checking for more exclusions and list this string to be excluded. Try this function out and tell if it does the trick?

;#####################################################################################################
;Function to loop through the array of exclusions & check if any of the exclusions exist in our string
;#####################################################################################################
Func _CheckExcludes()
    ;clear previous value for $excludes
    $excludes=0

    ; check the string against a list of exclusions (ie. URLs)
    For $i = 1 to Ubound($arrExcludes)-1
        $excludes = StringInStr($myCircuit, $arrExcludes[$i]) 

        ;if exclusion found, then stop checking
        If $excludes Then
            ;Msgbox ("","Exclusion Found At",$excludes)
            ExitLoop
        EndIf
    Next
EndFunc

....Or if you prefer to know how many different exclusions where found in the string, then you could do something like this:

;#####################################################################################################
;Function to loop through the array of exclusions & check if any of the exclusions exist in our string
;#####################################################################################################
Func _CheckExcludes()
    ;clear previous value for $excludes
    $excludes=0

    ; check the string against a list of exclusions (ie. URLs)
    Local $excludeFound
    For $i = 1 to Ubound($arrExcludes)-1
        $excludeFound = StringInStr($myCircuit, $arrExcludes[$i])
        If $excludeFound Then $excludes += 1
    Next
EndFunc

Realm

Edited by Realm
Added a second possible solution

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

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