Help using Instr Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Rieschiek
Posts: 15
Joined: 01 Dec 2022, 15:39

Help using Instr

Post by Rieschiek » 09 Dec 2022, 11:17

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

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Help using Instr

Post by Smile_ » 09 Dec 2022, 11:58

Maybe...

Code: Select all

We_Should_Go := !(results ~= "\Qmessaged you\E|\QFinds this text in file\E")
If (We_Should_Go)
    GoSub trigger1
Last edited by Smile_ on 09 Dec 2022, 12:35, edited 1 time in total.

Rieschiek
Posts: 15
Joined: 01 Dec 2022, 15:39

Re: Help using Instr

Post by Rieschiek » 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?

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Help using Instr

Post by Chunjee » 09 Dec 2022, 12:32

I like this format

Code: Select all

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

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

Rieschiek
Posts: 15
Joined: 01 Dec 2022, 15:39

Re: Help using Instr

Post by Rieschiek » 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.

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Help using Instr

Post by Chunjee » 09 Dec 2022, 16:16

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

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Help using Instr  Topic is solved

Post by Smile_ » 09 Dec 2022, 18:06

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

Rieschiek
Posts: 15
Joined: 01 Dec 2022, 15:39

Re: Help using Instr

Post by Rieschiek » 09 Dec 2022, 18:10

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
}

Rieschiek
Posts: 15
Joined: 01 Dec 2022, 15:39

Re: Help using Instr

Post by Rieschiek » 09 Dec 2022, 18:15

Thanks so much! I read the documentation for it and I couldn't get my head around it but you explanation worked!

Rieschiek
Posts: 15
Joined: 01 Dec 2022, 15:39

Re: Help using Instr

Post by Rieschiek » 09 Dec 2022, 19:14

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.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Help using Instr

Post by boiler » 09 Dec 2022, 19:25

Code: Select all

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

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

Rieschiek
Posts: 15
Joined: 01 Dec 2022, 15:39

Re: Help using Instr

Post by Rieschiek » 29 Dec 2022, 15:08

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?

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Help using Instr

Post by Smile_ » 29 Dec 2022, 15:31

Can you provide an example?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Help using Instr

Post by boiler » 29 Dec 2022, 16:09

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

Post Reply

Return to “Ask for Help (v1)”