examine a variable containing a string, and use array string to see 'if' any word from array string exists in variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

examine a variable containing a string, and use array string to see 'if' any word from array string exists in variable

Post by samt » 29 Jan 2023, 15:41

Not sure exactly how to word this but I'll give it a go.

1)a different script will generate a random sentence, store it in variable 'SearchHere'
2)use 'if' condition to see if any word in 'SearchHere' contains/matches any word in an array string (what I need help with)
3)if a word from array string exists in 'SearchHere', then 'if' condition executes, otherwise checks the next 'else if' condition
4)be able to check for any single word from multiple arrays using 1 'if' condition


Code: Select all


SearchHere := "This will contain a randomly generated string everytime script runs and will get searched to see if any word from it matches any word from arrays below"
StrArray := {"Any", "single", "word", "here", "can", "be", "matched"} ; just for example. Actual words would be like "Document7"
StrArray2 := {"Another", "list", "to", "check"}

; What I've tried so far but hasn't quite worked

if (SearchHere contains %StrArray%) ; search for any word from an array, hasn't worked yet
		{
		(statement)
		 }

                ; search for word from single or multiple arrays. Tried different versions here but none seem to work
                ; else if (InStr(StrArray, Document7))         (tried a different way to search for just one specific word from an array)
                ; else if "Document7" in %StrArray2%           (also tried it using brackets like ["Document8", "Doc8"])
                else if (SearchHere contains %StrArray% || %StrArray2%)  ; search for any word from multiple arrays
                {
               (statement2)
               }

    		else
           	{
               (statement3)
            }

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by mikeyww » 29 Jan 2023, 15:54

Code: Select all

; This script compares a string's words to array elements
#Requires AutoHotkey v1.1.33
str  := "This will contain a randomly generated string everytime script runs and will get "
      . "searched to see if any word from it matches any word from arrays below"
arr  := ["Any", "single", "word", "here", "can", "be", "matched"]
list := ""
For each, item in m := match(str, arr)
 list .= (list = "" ? "" : "`n") item
MsgBox, 64, % "Matches (N=" m.Count() ")", % list

match(str, arr) { ; Return array of matches
 match := []
 For each, word in arr
  RegExMatch(str, "i)\b" word "\b") && match.Push(word)
 Return match
}

Code: Select all

; This script compares a string's words to array elements
#Requires AutoHotkey v2.0
str  := "This will contain a randomly generated string everytime script runs and will get "
      . "searched to see if any word from it matches any word from arrays below"
arr  := ["Any", "single", "word", "here", "can", "be", "matched"]
list := ""
For each, item in m := match(str, arr)
 list .= (list = "" ? "" : "`n") item
MsgBox list, "Matches (N=" m.Length ")", 64

match(str, arr) { ; Return array of matches
 match := []
 For each, word in arr
  RegExMatch(str, "i)\b" word "\b") && match.Push(word)
 Return match
}

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by flyingDman » 29 Jan 2023, 16:09

This checks both arrays:

Code: Select all

SearchHere := "This will contain a randomly generated string everytime script runs and will get searched to see if any word from it matches any word from arrays below"
StrArray := ["Any", "single", "word", "here", "can", "be", "matched"] ; just for example. Actual words would be like "Document7"
StrArray2 := ["Another", "list", "to", "check"]

for a,b in [StrArray,StrArray2]
	for x,y in b
		if y in % strreplace(SearchHere," ",",")
			lst .= y "`n"
		else
			{}
msgbox % lst
14.3 & 1.3.7

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by mikeyww » 29 Jan 2023, 16:46

One generalizable advantage of regex over an InStr approach is that regex is already designed to find other kinds of word boundaries, such as punctuation. This may have no relevance if these strings never contain any such characters.

samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by samt » 30 Jan 2023, 20:00

mikeyww wrote:
29 Jan 2023, 15:54

Code: Select all

; This script compares a string's words to array elements
I'm having trouble getting those do this what I want. I probabaly didn't explain it well enough, or I just dont understand it.
I tried the scripts and searched elsewhere. It seems like I can't use an array string the way I want to, or I'm just a noob. I ended up coming up with my own solution and it works for me, and its done without arrays, which is fine to be honest.
This examines a string and if any specific words are in it (in brackets), then it will execute a statement, or else check the next expression and so on

Code: Select all

text := "a string with words like "Document7..""

 if text contains (Document7), (Document8)
		{
                 Run, program1
		msgbox,0, testing , % text
		 }

               else if text contains (Document1), (Document3), (Document5)
           	{
               Run, program1
                }

    		else
           	{
                 run, notepad.exe
                }

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by mikeyww » 30 Jan 2023, 20:11

OK. If you're saying that this script works, I'm just going to take your word for it!

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

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by boiler » 30 Jan 2023, 21:32

samt wrote: ...it works for me, and its done without arrays, which is fine to be honest.
This examines a string and if any specific words are in it (in brackets), then it will execute a statement, or else check the next expression and so on
Perhaps you're just lucky so far. There are cases where the text won't be found, like this one:

Code: Select all

text := "(Document3) won't be found here"
Maybe you won't have cases like that, but if you do, then it's a good exercise to identify why it won't.

samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by samt » 30 Jan 2023, 22:39

boiler wrote:
30 Jan 2023, 21:32
samt wrote: ...it works for me, and its done without arrays, which is fine to be honest.
This examines a string and if any specific words are in it (in brackets), then it will execute a statement, or else check the next expression and so on
Perhaps you're just lucky so far. There are cases where the text won't be found, like this one:

Code: Select all

text := "(Document3) won't be found here"
Maybe you won't have cases like that, but if you do, then it's a good exercise to identify why it won't.
You're right. It's just single words that I need to find, not sentences which don't seem to work

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

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by boiler » 30 Jan 2023, 23:51

samt wrote: You're right. It's just single words that I need to find, not sentences which don't seem to work
Well, no, you didn't reach the conclusion I was driving at. Sentences would work too if you set them up right. You didn't bite when I tried to get you to find why the example I showed you didn't work. It's because of the way the "contains" list works (as documented). Any spaces that you have are part of the search strings. So this line:

Code: Select all

else if text contains (Document1), (Document3), (Document5)

...should be:

Code: Select all

else if text contains (Document1),(Document3),(Document5)

Then it will find the (Document3) in the example I showed as well.

samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by samt » 31 Jan 2023, 00:06

boiler wrote:
30 Jan 2023, 23:51
samt wrote: You're right. It's just single words that I need to find, not sentences which don't seem to work
Well, no, you didn't reach the conclusion I was driving at. Sentences would work too if you set them up right. You didn't bite when I tried to get you to find why the example I showed you didn't work. It's because of the way the "contains" list works (as documented). Any spaces that you have are part of the search strings. So this line:

Code: Select all

else if text contains (Document1), (Document3), (Document5)

...should be:

Code: Select all

else if text contains (Document1),(Document3),(Document5)

Then it will find the (Document3) in the example I showed as well.
Oh sorry I didn't see the bottom sentence from your previous post. I got it mixed up, yes, trying to find a sentence in a sentence does work though it is not for my purposes. For my needs, I need those (Document7) words to be by themselves on their left-side, hence the space on the left side, otherwise if there's something attached to them on their left, it wont be what I'm looking for so the script purposely ignores those. So I have tested the words with spaces/no spaces on either side to experiment what works and ensured the script reacts properly. It took a bit of time since I'm new

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

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by boiler » 31 Jan 2023, 00:11

samt wrote: For my needs, I need those (Document7) words to be by themselves on their left-side, hence the space on the left side, otherwise if there's something attached to them on their left, it wont be what I'm looking for so the script purposely ignores those.
I see. That makes sense. Thanks for clarifying.

samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: examine a variable containing a string, and use array string to see 'if' any word from array string exists in variab

Post by samt » 31 Jan 2023, 00:15

boiler wrote:
31 Jan 2023, 00:11
samt wrote: For my needs, I need those (Document7) words to be by themselves on their left-side, hence the space on the left side, otherwise if there's something attached to them on their left, it wont be what I'm looking for so the script purposely ignores those.
I see. That makes sense. Thanks for clarifying.
I appreciate your taking the time to give help/tips 👍

Post Reply

Return to “Ask for Help (v1)”