comparing two arrays & flagging change

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

comparing two arrays & flagging change

29 Jun 2014, 19:43

I'm pretty darn sure this is simple using an array however I'm just not able to think it through...

On Mondays I'll get a list of email campaigns that are to be sent out. Each one has a Unique ID (in column 1 below) and is to be sent on a specific day. Often, later in the week, people will add campaigns to the calendar but not tell me. It would be really helpful if I had a script that
1) Read in each array
2) Used the first column of the data as a key across to two arrays (Note they may be out of order)
3) Compared the dates of each existing campaign (flagging if the date(second column) is different
4) flagged any IDs in data2 that are not in data1

When I say "flagged" having the Unique IDs displayed in a messagebox or copied to clipboard would be fine.

Also- just so you don't think I'm a total idiot, I'm only showing a few in my example below, typically we have about 30 campaigns which makes this process fairly tedious.

Thank you in advance for any and all help!

Being data1=
(
1572 6/17
2697 6/17
2728 6/17
2521 6/17
)

data2=
(
1572 6/10
2521 6/18
2697 6/17
9922 6/10
2728 6/17
)
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: comparing two arrays & flagging change

29 Jun 2014, 20:17

Basically you will need each campaign of data1 to be compared to each campaign in data2.
So you need two loops.
1st loop going over data1
In that loop
Split the line in id and date
Then start 2nd loop over data 2 (still in loop1)
Split line of data 2 in id and date
Compare loop1 I'd and date against these values.
Store the info you need
End loop 2
End loop 1
Show info
ciao
toralf
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: comparing two arrays & flagging change

29 Jun 2014, 20:19

Since you are more interested in the changes in data2 maybe you should go through data 2 with the first loop. Then you can flag new or changed info in data 2 items easier.
ciao
toralf
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: comparing two arrays & flagging change

29 Jun 2014, 20:22

If you say so... lol

I think you're right however I still don't know how to do this. For me the complexity is that I have that key to first match up, then compare. I guess I could just loop through each row and compare the index. If the index is the same, then compare the dates.

It just seems that somehow using an array or a data dictionary would be smarter I'm just over my head on figuring this out.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: comparing two arrays & flagging change

29 Jun 2014, 20:33

toralf- sorry I didn't see your first repy (just your second) I'll review your suggestion tomorrow.
Thank you!
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: comparing two arrays & flagging change

30 Jun 2014, 08:10

Not exactly elegant, but it gets the job done! Thanks for your help

Code: Select all

#SingleInstance, Force
#NoEnv
RAlt::Reload
RControl::
data1=
(
2390	6/17
1572	6/17
2709	6/17
2697	6/17
2499	6/17
2728	6/17
2521	6/17
)

data2=
(
2728	6/17
1572	6/10
2390	6/17
2499	6/17
2521	6/18
8888	6/17
2697	6/17
2709	6/17
9999	6/17
)

;**********************Create array off Data2*********************************
Loop, Parse, Data2, `n, `r
{ 
DataArray2_%A_Index% := A_LoopField ;creating an array
DataArray2_CT := (A_Index) 
  }

Loop, %DataArray2_CT%  ;loop through DataArray2
{
StringSplit, Array2_, DataArray2_%A_Index%, %A_Tab% ;splits out the columns

;**********************loop through DataArray1*********************************
Loop, Parse, Data1, `n, `r
{ 
DataArray1_%A_Index% := A_LoopField ;creating an array
DataArray1_CT := (A_Index) 
}

Match:=0 ;set match to equal 0
Loop, %DataArray1_CT%  ;this loops through by index and splits out the columns
{
StringSplit, Array1_, DataArray1_%A_Index%, %A_Tab%
  if (Array1_1=Array2_1) 
		match:=1 ;change matched value to show that it matched soemthing
		
  if (Array1_1=Array2_1) and (Array1_2 != Array2_2) ;see if date changed
		Date_Changed.=Array1_1 " on " Array1_2 " and " Array2_2 "`r"
}
  If (Match=0)
	New_Campaigns.=Array2_1 "`r`n"
}

;**********************show results*********************************
MsgBox % "New Campaigns: `r`n" . New_Campaigns
       . "`n`rcampaigns that dates changed`r`n" Date_changed

return
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: comparing two arrays & flagging change

30 Jun 2014, 11:07

Code: Select all

data1=
(
1572 6/17
2697 6/17
2728 6/17
2521 6/17
4545 6/17
)

data2=
(
1572 6/10
2521 6/18
2697 6/17
9922 6/10
2728 6/17
)

for key, var in ["data1", "data2"] {
	o%var% := {}
	Loop, Parse, %var%, `n
		SpacePos := InStr(A_LoopField, A_Space)
		, o%var%["_" SubStr(A_LoopField, 1, SpacePos - 1)] := SubStr(A_LoopField, SpacePos + 1)
}

for key, val in odata1
	if (odata2[key] = val)
		No_Change .= SubStr(key, 2) " " odata2.Remove(key) "`n"
	else if (odata2[key] != "")
		Date_changed .= SubStr(key, 2) " " odata2.Remove(key) "`n"
	else
		Removed .= SubStr(key, 2) " " val "`n"

for key, val in odata2
	New_Campaigns .= SubStr(key, 2) " " val "`n"

MsgBox, % "No Change:`n" No_Change "`n`nNew Campaigns:`n" New_Campaigns "`n`nDate changed:`n" Date_changed "`n`nRemoved:`n" Removed
Edit: Added "removed" to detect campaigns that are in the first list but not the second.
Last edited by kon on 30 Jun 2014, 11:44, edited 1 time in total.
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: comparing two arrays & flagging change

30 Jun 2014, 11:13

Very cool kon. I'll play with it a bit later. I'm actually grabbing the data from Excel so I think this approach might be a little easier to work-in from a programatic standpoint.
Thank you!
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: comparing two arrays & flagging change

30 Jun 2014, 15:29

Spoiler
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: comparing two arrays & flagging change

30 Jun 2014, 18:48

Even cooler!

Not only did yall answer my question but I'm going to study the various methods and make sure I learn how they are working. This last one is hurting my brain but I love the simplicity/brevity of the code!

Thank you all!
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: comparing two arrays & flagging change

30 Jun 2014, 23:42

@ AlphaBravo
Nice!
It's cleaner; I don't know why I didn't use StrSplit() in mine... I suppose that I just thought about Instr()/SubStr() and didn't second guess it.
I did some speed tests, and yours is also faster. ~10% as far as I can tell overall. But, the two for-loops are MUCH faster in your script. My first thought was that it is because you don't call SubStr() or Array.Remove(key), but removing that from my script didn't actually increase the speed much. So I am left with the hypothesis that the order and type of operations you use are the cause.
Thoughts?
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: comparing two arrays & flagging change

01 Jul 2014, 05:28

The substr() in the loop might be the cause.
Just a guess.
ciao
toralf
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: comparing two arrays & flagging change

01 Jul 2014, 05:31

Maybe because your keys are strings "_1234" and the others are numbers.
ciao
toralf
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: comparing two arrays & flagging change

01 Jul 2014, 09:26

toralf wrote:The substr() in the loop might be the cause.
Just a guess.
that will be my guess too.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: marypoppins_1, mikeyww, RussF and 138 guests