Comparing two variables not in the same order

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
frexum
Posts: 7
Joined: 23 Mar 2018, 17:02

Comparing two variables not in the same order

23 Mar 2018, 17:25

Hello,

At work I have to confirm data entered from a Word document matches what's built into a website. The problem is that they aren't in the exact same order. Another issue is that the way the Word document is set in table format, I can't just copy the numbers I need all at once since it grabs a bunch of other useless information. I've built a couple hotkeys that will copy the information from the website and save that as a variable, and then the second code will check if one highlighted item is in that variable. I'm wondering if there's a way for me to add multiple items to a variable (or maybe an array?) one by one from the Word document and then have the code compare all of those to the website saved variable and finally if any of them are missing, a message box would display any missing numbers.

Here's what I have so far.

Code: Select all

#p:: 
sendinput,^c 
sleep,50 
drugset:= clipboard 
return 

#i:: 
sendinput,^c 
sleep,50 
Haystack := drugset 
Needle := clipboard 
If InStr(Haystack, Needle) 
        MsgBox, Good to go 
Else 
        Msgbox, No bueno 
return 
So Windows+p makes the initial variable named drugset. Windows+i will copy a number from the Word document that I have highlighted and then do the whole needle/haystack thing. The sleeps probably aren't necessary but I'm pretty new to this so it's my way of doing a hackjob :lol:


Here's an example of what is on the website when it gets copied



GPI


50280020001620


GPI


50250070102010


GPI


50250070102020



GPI


50280035102120


GPI


50280035102130


GPI


5025003500E420


GPI


50280050201620




A couple of things that crossed my mind were to create an array and then append the array with each new number from the Word document to that array. Then somehow loop through that and compare it to the drugset variable? Any suggestions/tips are greatly appreciated.


Thanks!
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Comparing two variables not in the same order

24 Mar 2018, 02:20

While it might be possible to access your Word document using COM, what about to save a work-copy in plain text format and attach it here so the regex-guys can have a look at it?
Once those "numbers" are extracted you can use AHK's Sort-command to compare/sort them.
frexum
Posts: 7
Joined: 23 Mar 2018, 17:02

Re: Comparing two variables not in the same order

24 Mar 2018, 09:44

I can't quite wrap my head around the COM stuff yet. I didn't know the Sort function existed.



Code: Select all

Brand (generic) GPI  Quantity Limit
Bevyxxa (betrixaban)
40 mg capsule 83370018200120	43 capsules/42 days
80 mg capsule 83370018200140	43 capsules/42 days
Eliquis (apixaban)
2.5 mg tablet  83370010000320		2 tablets/day 
5 mg tablet  83370010000330		4 tablets/day 
Pradaxa (dabigatran)
75 mg capsule 83337030200120	2 capsules/day
110 mg capsule 83337030200130	71 capsules/90 days
150 mg capsule 83337030200140	2 capsules/day
Savaysa (edoxaban)
15 mg tablet 83370030200315	1 tablet/day
30 mg tablet 83370030200330	1 tablet/day
60 mg tablet 83370030200350	1 tablet/day
Xarelto (rivaroxaban)
Starter Pack 8337006000B720	51 tablets/30 days
10 mg tablets 8337006000320	1 tablet/day
15 mg tablets 8337006000330	2 tablets/day
20 mg tablets 8337006000340	1 tablet/day





This is what the Word table ends up looking like as text. Usually they're 13 digits long but sometimes they might be wildcarded like 8337006000*** but from what I'm reading about Sort I think it still might put the number sets in the same order. So you're thinking Regex the Word table to just grab the GPI numbers, save that as a variable and sort it. Sort the other number list that it's comparing to, and then do the Needle/Haystack thing?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Comparing two variables not in the same order

24 Mar 2018, 15:19

So you're thinking Regex the Word table to just grab the GPI numbers, save that as a variable and sort it. Sort the other number list that it's comparing to, and then do the Needle/Haystack thing?
Yep. And once that has been accomplished you could think about how you can (auto)extract the data from the webpage :thumbup:
frexum
Posts: 7
Joined: 23 Mar 2018, 17:02

Re: Comparing two variables not in the same order

24 Mar 2018, 16:48

I appreciate you checking back in on me. I've been trying to work through that pathway but hit a wall with the RegEx. Here's what I have so far

Code: Select all

#p:: 
sendinput,^c 
sleep,50 
drugset:= StrReplace(clipboard, "GPI", "", "All") 
Sort, drugset, [N]
return 

#i:: 
sendinput,^c 
sleep,50 
Haystack := drugset 
Needle := clipboard 
If InStr(Haystack, Needle) 
        MsgBox, Good to go 
Else 
        Msgbox, No bueno 
return 
So I took out all of the "GPI" from the website variable and then sorted the numbers. Then it came time to try and work through the Word document and use RegEx to just grab the GPI numbers but I couldn't figure out the best way to do it. My first thought was to just grab all the numbers that were between 5-13 digits long but sometimes alphabetical characters pop in so I don't think that works. I also had a tough time figuring out how to get the RegEx to go through multiple lines and also save each instance of what I wanted as a new variable/output. I'm also not sure I know how to solve the last step when comparing the two, I would need it to specifically call out what numbers aren't matching? Thanks again for your help
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Comparing two variables not in the same order

24 Mar 2018, 17:10

While waiting for regex-support (sorry, can't help here) you could have a regex-free attempt using StringSplit against the space/tab characters. :)
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Comparing two variables not in the same order

24 Mar 2018, 19:56

give this a try:

Code: Select all

H =
(
Brand (generic) GPI  Quantity Limit
Bevyxxa (betrixaban)
40 mg capsule 83370018200120**	43 capsules/42 days
80 mg capsule 83370018200140	43 capsules/42 days
Eliquis (apixaban)
2.5 mg tablet  83370010000320		2 tablets/day 
5 mg tablet  83370010000330		4 tablets/day 
Pradaxa (dabigatran)
75 mg capsule 83337030200120	2 capsules/day
110 mg capsule 83337030200130	71 capsules/90 days
150 mg capsule 83337030200140	2 capsules/day
Savaysa (edoxaban)
15 mg tablet 83370030200315	1 tablet/day
30 mg tablet 83370030200330	1 tablet/day
60 mg tablet 83370030200350	1 tablet/day
Xarelto (rivaroxaban)
Starter Pack 8337006000B720	51 tablets/30 days
10 mg tablets 8337006000320	1 tablet/day
15 mg tablets 8337006000330	2 tablets/day
20 mg tablets 8337006000340	1 tablet/day
)

while pos := RegExMatch(H, "\w+(?=\**\h+\d+\h+\w+/)", m, A_Index=1?1:pos+StrLen(m))
	res .= m "`n"

MsgBox % res
frexum
Posts: 7
Joined: 23 Mar 2018, 17:02

Re: Comparing two variables not in the same order

25 Mar 2018, 13:53

That appears to work, thanks!

I added that to the code and then had both sets of numbers sorted and then tried to compare them and from my untrained eye they look the same but the code says they aren't. Is the code not reading the two sets the way I think it is?


Here's the updated code

Code: Select all

#p:: 
sendinput,{control down}c 
sleep,50 
sendinput,{control up}
drugset:= StrReplace(clipboard, "GPI", "", "All") 
Sort, drugset, [N]
StringReplace,drugset,drugset,`n,,A
sleep,50
return 

#i:: 
sendinput,{control down}c 
sleep,50 
sendinput,{control up}
umds = %clipboard%
while pos := RegExMatch(umds, "\w+(?=\**\h+\d+\h+\w+/)", m, A_Index=1?1:pos+StrLen(m))
	res .= m "`n"
Sort, res, [N]
Haystack := drugset 
Needle := res
If InStr(Haystack, Needle) 
        MsgBox, Good to go 
Else 
        Msgbox, No bueno 
sleep,50
return 

^t::
sleep,25
umds = %clipboard%
while pos := RegExMatch(umds, "\w+(?=\**\h+\d+\h+\w+/)", m, A_Index=1?1:pos+StrLen(m))
	res .= m "`n"
sleep,50
return

^y::
sleep,25
sendinput %drugset%
sleep,50
return

^u::
sleep,25
Sort, res, [N]
sendinput %res%
sleep,25
return
I created the last couple codes just to test what the output looked like and these are the two sets of numbers
From Control-Y

Code: Select all

8337006000B720
8337006000320
8337006000330
8337006000340
83337030200120
83337030200130
83337030200140
83370010000320
83370010000330
83370018200120
83370018200140
83370030200315
83370030200330
83370030200350


And Control-U (numbers from the table)

Code: Select all

8337006000B720
8337006000320
8337006000330
8337006000340
83337030200120
83337030200130
83337030200140
83370010000320
83370010000330
83370018200120
83370018200140
83370030200315
83370030200330
83370030200350


User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Comparing two variables not in the same order

25 Mar 2018, 20:09

you may have white space characters in there

Code: Select all

If (RegExReplace(drugset, "\s") = RegExReplace(res, "\s"))
	MsgBox, Good to go 
Else 
	Msgbox, No bueno
frexum
Posts: 7
Joined: 23 Mar 2018, 17:02

Re: Comparing two variables not in the same order

25 Mar 2018, 21:18

¡It worked! :bravo: Thank you so much! I need to work up on my Regex skills

Do you have any idea how I might be able to identify any Needles that didn't get matched in the Haystack? I found this thread with a similar question but it's over my head and I don't think they ended up with any negation of Regex .

https://autohotkey.com/board/topic/1712 ... xpression/

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: OrangeCat, pgeugene, sanmaodo, zerox and 297 guests