Jump to content

AnyStringInStr()


 Share

Recommended Posts

I would like a better way of doing this; one that wouldn't need re-compiling to change the number of parameters. Here's what I have this far:

$in = InputBox("Prompt", "Enter a time code to sleep after no input has been detected." & @CRLF & "Example: ' 1h15m0s ' = 1 hour, 15 minutes.")

If Not AnyStringInStr($in, "h", "m", "s") Then
    die("None of the strings exist.")
Else
    die("One of the strings exists.")
EndIf

Func AnyStringInStr($a, $b, $c, $d)
    $truth = False
    If StringInStr($a, $b) Then
        $truth = True
    ElseIf StringInStr($a, $c) Then
        $truth = True
    ElseIf StringInStr($a, $d) Then
        $truth = True
    EndIf
    Return $truth
EndFunc

Func die($y, $x='', $w=262160, $z=0)
    MsgBox($w, $x, $y, $z)
    close()
EndFunc

Func close()
    Exit
EndFunc

I was thinking something more like

$in = InputBox("Prompt", "Enter a time code to sleep after no input has been detected." & @CRLF & "Example: ' 1h15m0s ' = 1 hour, 15 minutes.")

If Not AnyStringInStr($in, "hms") Then
    die("None of the strings exist.")
Else
    die("One of the strings exists.")
EndIf

Func AnyStringInStr($a, $b)
    $truth = False
    For $i = 1 To StringLen($b)
        $c = StringLeft($b, $i)
        $c = StringRight($c, 1)
        If StringInStr($a, $c) Then
            $truth = True
            ExitLoop
        EndIf
    Next
    Return $truth
EndFunc

Func die($y, $x='', $w=262160, $z=0)
    MsgBox($w, $x, $y, $z)
    close()
EndFunc

Func close()
    Exit
EndFunc

Is there a better way to do this? Advice is appreciated.

Edited by cembry90

AutoIt Stuff:

 

UDFs: {Grow}

Link to comment
Share on other sites

Could you possible give a quick explanaition of what it is supposed to do?

EDIT:

Scrap that, I see now.

EDIT 2:

Looks like it could be a job for stringregexp, If Im right in thinking you want to take the number of hours minutes and seconds, then calculate the sleep time in milliseconds.

I'm a stringregexp drop out though Im afraid.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

$in = InputBox("Prompt", "Enter a time code to sleep after no input has been detected." & @CRLF & "Example: ' 1h15m0s ' = 1 hour, 15 minutes.")

If Not AnyStringInStr($in, "hms") Then
    die("None of the strings exist.")
Else
    die("One of the strings exists.")
EndIf

Func AnyStringInStr($a, $b)
    StringSplit($a,$b)
    If @error Then
        Return False
    Else
        Return True
    EndIf
EndFunc

Func die($y, $x='', $w=262160, $z=0)
    MsgBox($w, $x, $y, $z)
EndFunc

I leave it to others to demonstrate the regex-ing techniques.

Hi ;)

Link to comment
Share on other sites

Guess this is what you're looking for:

Func AnyStringInStr($a, $b)
    $truth = False
    Local $str_b = StringSplit($b, "")
    For $i = 1 To $str_b[0]
        If StringInStr($a, $str_b[$i]) Then $truth = True
    Next
    Return $truth
EndFunc

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Could you possible give a quick explanaition of what it is supposed to do?

EDIT:

Scrap that, I see now.

EDIT 2:

Looks like it could be a job for stringregexp, If Im right in thinking you want to take the number of hours minutes and seconds, then calculate the sleep time in milliseconds.

I'm a stringregexp drop out though Im afraid.

What I'm wanting to do is make a timer that gets the user's inputs (an amount of time) and waits until the keyboard and mouse are inactive for that amount of time, then performs an action; an artificial screensaver of sorts.

Here's the complete code I have this far:

#include <Misc.au3>
HotKeySet("{numpadadd}", "close")

Global $dll = DllOpen('user32.dll')
$time = InputBox("Prompt", "Enter a number ([?h][?m][?s]) to sleep after no input has been detected. Please note, this does NOT work when the computer is at the login screen." & @CRLF & "Example: " & @TAB & "' 1h15m0s ' =" & " 1 hr, 15 mins" & @CRLF & @TAB & @TAB & "' 123 ' =" & " 2 mins, 3 secs")  ; do something after this number of seconds

If Not AnyStringInStr($time, "hms") Then;, "m", "s") Then
    $time = BreakMeUp($time)
EndIf

$h = StringSplit($time, "h")
If $h[0] >=2 Then
    $time = $h[2]
    $h = $h[1]
Else
    $h = 0
EndIf

$m = StringSplit($time, "m")
If $m[0] >=2 Then
    $time = $m[2]
    $m = $m[1]
Else
    $m = 0
EndIf

$s = StringSplit($time, "s")
If $s[0] >=2 Then
    $time = $s[2]
    $s = $s[1]
Else
    $s = 0
EndIf

$time = 3600*$h + 60*$m + $s

$time = StringRegExpReplace($time, "[^\d.-]|([{0-9,1}^\A-])[^\d.]", "\1")

If $time = 0 Then
    die("Not a valid time code.", "Error")
EndIf

$count = 0
While 1
    $sec = @SEC
    $pos = pos()
    While $sec = @SEC
        Sleep(1)
        $perc = Round(100*($count/$time), 2)
        If $time - $count <= 3 Then
            ToolTip("( " & $count & " / " & $time & " ) = " & $perc & "%", ((pos(0)/pos(0))-1), ((pos(0)/pos(0))-1)+20, "Alert")
        Else
            ToolTip("( " & $count & " / " & $time & " ) = " & $perc & "%", ((pos(0)/pos(0))-1), ((pos(0)/pos(0))-1)+20, "Info")
        EndIf
        If HasMoved($pos) Then $count = 0
    WEnd
    $count += 1
    If $count >= $time Then
        Action()
        $count = 0
    EndIf
WEnd

Func die($y, $x='', $w=262160, $z=0)
    MsgBox($w, $x, $y, $z)
    close()
EndFunc

Func close()
    Exit
EndFunc

Func HasMoved($pos)
    $truth = False
    If $pos <> pos() Then
        $truth = True
    EndIf
    For $i = 1 To 255
        $j = Hex($i, 2)
        If _IsPressed($j, $dll) Then
            $truth = True
        EndIf
    Next
    Return $truth
EndFunc

#cs
Func AnyStringInStr($a, $b, $c, $d)
    $truth = False
    If StringInStr($a, $b) Then
        $truth = True
    ElseIf StringInStr($a, $c) Then
        $truth = True
    ElseIf StringInStr($a, $d) Then
        $truth = True
    EndIf
    Return $truth
EndFunc
#ce

Func AnyStringInStr($a, $b)
    $truth = False
    For $i = 1 To StringLen($b)
        $c = StringLeft($b, $i)
        $c = StringRight($c, 1)
        If StringInStr($a, $c) Then
            $truth = True
            ExitLoop
        EndIf
    Next
    Return $truth
EndFunc

Func BreakMeUp($a)
    Local $h=0, $m=0, $s=0
    If $a >= 3600 Then
        $h = Floor($a/3600)
        $a = $a - ($h*3600)
    EndIf
    If $a >= 60 Then
        $m = Floor($a/60)
        $a = $a - ($m*60)
    EndIf
    $s = $a
    $b = $h & "h" & $m & "m" & $s & "s"
    Return $b
EndFunc

Func pos($a=-1)
    If $a = -1 Then
        Return MouseGetPos(0) & "," & MouseGetPos(1)
    Else
        If $a = 0 Then
            Return MouseGetPos(0)
        ElseIf $a = 1 Then
            Return MouseGetPos(1)
        EndIf
    EndIf
EndFunc

Func Action()
    Beep(1000, 200)
    MsgBox(262144, "Alert", "Action!")
EndFunc

Guess this is what you're looking for:

Func AnyStringInStr($a, $b)
    $truth = False
    Local $str_b = StringSplit($b, "")
    For $i = 1 To $str_b[0]
        If StringInStr($a, $str_b[$i]) Then $truth = True
    Next
    Return $truth
EndFunc

There's many ways to do it apparently, but I was just wondering what the simplest way was. Your example is a variation of what I did with string trimming. :-) Nice one, btw. Edited by cembry90

AutoIt Stuff:

 

UDFs: {Grow}

Link to comment
Share on other sites

Try this.

Global $UsedString = "This is a test."

$Return1 = _AnyStrInString($UsedString, "aht")
$Return2 = _AnyStrInString($UsedString, "is|test", "|")
$Return3 = _AnyStrInString($UsedString, "bzo")
$Return4 = _AnyStrInString($UsedString, "autoit|random|words", "|")

MsgBox(0, "", "Results: " & @CRLF & "$Return1: " & $Return1 & @CRLF & _
                                                "$Return2: " & $Return2 & @CRLF & _
                                                "$Return3: " & $Return3 & @CRLF & _
                                                "$Return4: " & $Return4)

Func _AnyStrInString($sString, $sSearch, $sSplitChar = "")
    Local $aSplit = StringSplit($sSearch, $sSplitChar)

    For $i = 1 To $aSplit[0]
        If (StringInStr($sString, $aSplit[$i])) Then Return True
    Next

    Return False
EndFunc

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

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