Find matching words between n number of variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sn0365
Posts: 25
Joined: 03 Aug 2018, 12:59

Find matching words between n number of variables

24 Sep 2018, 15:36

Hello,

I urgently need some help, in finding matching words between "n" number of variables. I am creating a Gui, which will give me multiple variable containing text.

The first and the last variable is known, but the number of variables between the 1st and the last variable is not constant. It can vary based on the user choice.

I want a script, that must identify the numbers of variables between the 1st and the last variable and then find the matching words between all the variables.

I have a script that is able to find the matching words between 4 variables. But The user can input any number of variables between the Initial and Final variable.


Example Script for finding matching words -

Code: Select all

initial = (A1 - Superior lateral to medial MEGA ,[P-10])
var1 = (A2 - SUPERIOR MEGA [P-10])
var2 = (A3 - SUPERIOR MEGA [P-10])
final= (A4 - SUPERIOR MEGA [P-10])
 

Variable1 := initial
Variable2 := var1
Variable3 := var2
Variable4 := final

send, Initial - %Variable1% `n
send, Var1 - %Variable2% `n
send, Var2 - %Variable3% `n
send, Final - %Variable4% `n

send, `n

Loop, Parse, Variable1, % A_Space,",-()[];"
{
	Word1 := A_LoopField

	Loop, Parse, Variable2, % A_Space
	{
		Word2 := A_LoopField

				Loop, Parse, Variable3, % A_Space
				{
					Word3 := A_LoopField

							Loop, Parse, Variable4, % A_Space
							{
								Word4 := A_LoopField


								If (Word1 = Word2) && (Word1 = Word3) && (Word1 = Word4) {
								Output .= Word1 " "
								Variable1 := RegExReplace(Variable1, "\b" Word1 "\b")
								Variable2 := RegExReplace(Variable2, "\b" Word2 "\b")
								Variable3 := RegExReplace(Variable3, "\b" Word3 "\b")
								Variable4 := RegExReplace(Variable4, "\b" Word4 "\b")
								}


							}
				}
	}


}


winactivate,Untitled - Notepad
send, Common Word - %Output% `n

send,`n


Output := RegExReplace(Output, "^\s+|\s+$", "")

Variable1 := RegExReplace(Variable1, "\,", "")

Variable1 := RegExReplace(RegExReplace(Variable1, "\. {1," X "}(?! )", ".  "), "[^ \.]\K {1," X "}(?! )", " ")

Variable2 := RegExReplace(RegExReplace(Variable2, "\. {1," X "}(?! )", ".  "), "[^ \.]\K {1," X "}(?! )", " ")

Variable3 := RegExReplace(RegExReplace(Variable3, "\. {1," X "}(?! )", ".  "), "[^ \.]\K {1," X "}(?! )", " ")

Variable4 := RegExReplace(RegExReplace(Variable4, "\. {1," X "}(?! )", ".  "), "[^ \.]\K {1," X "}(?! )", " ")


send, Edited Initial - %Variable1% `n
send, Edited var1 - %Variable2% `n
send, Edited var2 - %Variable3% `n
send, Edited Final - %Variable4% `n
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Find matching words between n number of variables

24 Sep 2018, 19:26

this is based on the assumption that all n number of variables are named var1, var2, etc., they are all in sequence and all contain something.

Code: Select all

initial = (A1 - Superior lateral to medial MEGA ,[P-10])
var1 = (A2 - SUPERIOR MEGA [P-10])
var2 = (A3 - SUPERIOR MEGA [P-10])
var3 = (Axx - SUPERIOR MEGA [P-10])
final= (A4 - SUPERIOR MEGA [P-10])

vars := ["initial", "final"]
matching := []

while (var%A_Index% <> "")
{
	x = var%A_Index%
	vars.push(x)
}

for n, var in vars
{
	varContents := RegExReplace(%var%, "\[.*?\]")			; remove [p-10] stuff
	for m, word in StrSplit(varContents, [A_Space, ",", "-", "(", ")", "[", "]", ";"])
		if word
		{
			if CheckCommon(Word, vars)
				matching[word] := true
		}
}
for word in matching
	res .= word ", "
MsgBox % Trim(res, ", ")
return

CheckCommon(Word, vars){
	for n, var in vars
		if !InStr(%var%, word)
			return false		
	return true
}
sn0365
Posts: 25
Joined: 03 Aug 2018, 12:59

Re: Find matching words between n number of variables

25 Sep 2018, 03:43

Dear AlphaBravo,

Thank you for your help.

Can you please add the part that once the common word has been found, it deletes it from all the initial variables.

regards
AlphaBravo wrote:this is based on the assumption that all n number of variables are named var1, var2, etc., they are all in sequence and all contain something.

Code: Select all

initial = (A1 - Superior lateral to medial MEGA ,[P-10])
var1 = (A2 - SUPERIOR MEGA [P-10])
var2 = (A3 - SUPERIOR MEGA [P-10])
var3 = (Axx - SUPERIOR MEGA [P-10])
final= (A4 - SUPERIOR MEGA [P-10])

vars := ["initial", "final"]
matching := []

while (var%A_Index% <> "")
{
	x = var%A_Index%
	vars.push(x)
}

for n, var in vars
{
	varContents := RegExReplace(%var%, "\[.*?\]")			; remove [p-10] stuff
	for m, word in StrSplit(varContents, [A_Space, ",", "-", "(", ")", "[", "]", ";"])
		if word
		{
			if CheckCommon(Word, vars)
				matching[word] := true
		}
}
for word in matching
	res .= word ", "
MsgBox % Trim(res, ", ")
return

CheckCommon(Word, vars){
	for n, var in vars
		if !InStr(%var%, word)
			return false		
	return true
}
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Find matching words between n number of variables

25 Sep 2018, 09:29

Here you go, also made few changes

Code: Select all

initial = (A1 - Superior lateral to medial MEGA ,[P-10])
var1 = (A2 - SUPERIOR MEGA [P-10])
var2 = (A3 - SUPERIOR MEGA [P-10])
var3 = (Axx - SUPERIOR MEGA [P-10])
final= (A4 - SUPERIOR MEGA [P-10])

vars := ["initial", "final"]
matching := []

while (var%A_Index% <> "")
{
	x = var%A_Index%
	vars.push(x)
}

for n, var in vars
{
	varContents := RegExReplace(%var%, "\[.*?\]")			; remove [p-10] stuff
	for m, word in StrSplit(varContents, [A_Space, ",", "-", "(", ")", "[", "]", ";"])
		if (word <> "") && CheckCommon(Word, vars)
			matching[word] := true
}
for word in matching
	needle .= word "|"
needle := "i)\b(" Trim(needle, "|") ")\b"

for n, var in vars
{
	%var% := RegExReplace(%var%, needle)					; remove common words
	%var% := RegExReplace(%var%, "\h\K\h+")					; remove extra white spaces
	MsgBox % var " = " %var%
}
return

CheckCommon(Word, vars){
	for n, var in vars
		if !(%var% ~= "i)\b" word "\b")
			return false		
	return true
}
sn0365
Posts: 25
Joined: 03 Aug 2018, 12:59

Re: Find matching words between n number of variables

25 Sep 2018, 10:36

Thank you very much....

I need to extract the common words.,,,I did add an additional variable

output .= word " " after needle .= word "|"

Code: Select all

for word in matching
	needle .= word "|"
	output .= word " "
needle := "i)\b(" Trim(needle, "|") ")\b
"

when I do send,%output% ....i only get Superior as the common word whereas it should give me Superior Mega
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Find matching words between n number of variables

25 Sep 2018, 12:06

Code: Select all

for word in matching
{
	needle .= word "|"
	output .= word " "
}
needle := "i)\b(" Trim(needle, "|") ")\b

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750 and 243 guests