Page 1 of 1

Help using Instr

Posted: 09 Dec 2022, 11:17
by Rieschiek
Hello, I'm very new to AHK and slowly getting used to how some things work. One thing I can't seem to get my head around is Instr, I am parsing a file and action based on text trigger.

I currently have this which works great. But I want it to fail/not trigger if it still includes the words but includes certain words. For example if it contains the word "messaged you" as well as "Finds this text in file" it should not gosub trigger1.

Code: Select all

trigger1 = Finds this text in file                    
If InStr(results, trigger1)
gosub trigger1

Re: Help using Instr

Posted: 09 Dec 2022, 11:58
by Smile_
Maybe...

Code: Select all

We_Should_Go := !(results ~= "\Qmessaged you\E|\QFinds this text in file\E")
If (We_Should_Go)
    GoSub trigger1

Re: Help using Instr

Posted: 09 Dec 2022, 12:26
by Rieschiek
Thanks for your prompt response. I don't understand how this will ignore if it contains "messaged you", what does the exclamation mark do?

Re: Help using Instr

Posted: 09 Dec 2022, 12:32
by Chunjee
I like this format

Code: Select all

haystack := "some long file or huge string"
needle := "some long"                   

If (InStr(haystack, needle)) {
    ; do something
}

Re: Help using Instr

Posted: 09 Dec 2022, 14:24
by Rieschiek
I like the format of this, but I want to know how to stop this from triggering if it included certain words even though it contained the trigger word.

Re: Help using Instr

Posted: 09 Dec 2022, 16:16
by Chunjee
Rieschiek wrote:
09 Dec 2022, 14:24
I like the format of this, but I want to know how to stop this from triggering if it included certain words even though it contained the trigger word.
example? Sounds like you will need ahk's AND and OR logic.

https://www.autohotkey.com/docs/Variables.htm#Operators

Re: Help using Instr  Topic is solved

Posted: 09 Dec 2022, 18:06
by Smile_
Rieschiek wrote:
09 Dec 2022, 12:26
Thanks for your prompt response. I don't understand how this will ignore if it contains "messaged you", what does the exclamation mark do?
~= used to determine whether a string contains a pattern (regular expression).
\Q...\E used to treat a needle as a literal text.
! is a negation symbol. Example !1 = 0 & !0 = 1 also !True = False & !False = True (Not and ! are the same, Not True = False also correct)

So if results contains "messaged you" or "Finds this text in file", (results ~= "\Qmessaged you\E|\QFinds this text in file\E") should return True (or 1).
But we want to GoSub trigger1 in the opposite case, so I added a ! to invert the result and puted it in We_Should_Go which I used later in If (We_Should_Go) or If (We_Should_Go = True).

Re: Help using Instr

Posted: 09 Dec 2022, 18:10
by Rieschiek
Thanks Chunjee,

The following should not trigger because it contains "Messaged you" or "Says":
Messaged you, "Turn off"

Whereas this should trigger (In my case it is a system message instead of a message coming from someone):
Turn off

This is what I would be using currently:

Code: Select all

trigger1 = Turn off
If (InStr(trigger1)){
GoSub trigger1
}

Re: Help using Instr

Posted: 09 Dec 2022, 18:15
by Rieschiek
Thanks so much! I read the documentation for it and I couldn't get my head around it but you explanation worked!

Re: Help using Instr

Posted: 09 Dec 2022, 19:14
by Rieschiek
Thanks all, it is working now as intended!

Code: Select all

triggerstop1 = a
triggerstop2 = b
triggerstop3 = c


trigger1 = x
If InStr(results, triggerstop1){
return
}
If InStr(results, triggerstop2){
return
}
If InStr(results, triggerstop3){
return
} 
else
If InStr(results, trigger1){
GoSub trigger1
}

Is there a way to make triggerstop1, triggerstop2, triggerstop3 all in oneline? I tried with %triggerstop% but it didn't seem to work.

Re: Help using Instr

Posted: 09 Dec 2022, 19:25
by boiler

Code: Select all

triggerstop1 := "a", triggerstop2 = "b", triggerstop3 = "c"
results := "cup"

if results contains %triggerstop1%,%triggerstop2%,%triggerstop3%
	MsgBox, Yes
else
	MsgBox, No

Re: Help using Instr

Posted: 29 Dec 2022, 15:08
by Rieschiek
Thanks for your comments guys it has been a great help. They all work, but unfortunately I have one with 72 different variant and if it's in the instr it seems to give me an error saying "expression too long".

Is there a way I can condense it?

Re: Help using Instr

Posted: 29 Dec 2022, 15:31
by Smile_
Can you provide an example?

Re: Help using Instr

Posted: 29 Dec 2022, 16:09
by boiler
You can use the if...contains approach with a variable that contains a long string. Here's a demo with more than 72 items:

Code: Select all

; this part just builds the comma-separated list:
loop, 3 {
	i := A_Index
	loop, 26
		list .= "," . Chr(96 + A_Index) . i
}
list := SubStr(list, 2) ; remove initial comma
MsgBox, % list

; check to see if the value of contained in 'results' is found within the listed items:
results := "g3"
if results contains %list%
	MsgBox, Yes
else
	MsgBox, No

Use in instead of contains to match the contents of results to an item in the list rather than seeing if an item in the list contains the contents of results. That is to say that contains is analogous to InStr(), while in is analogous to =.