Comparing 2 Lists

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
Erhull3287
Newbie
Posts: 10
Joined: Fri Apr 30, 2010 3:10 pm

Comparing 2 Lists

Post by Erhull3287 » Mon May 17, 2010 4:55 pm

I am trying to compare two lists that are found in two text files. I have been using Readln to read through the files but am having trouble coming up with a way to compare it against another list and tell me which numbers in the lists do not match up to each other. The lists look something like this:

List one:

0021545
0021584
0023625
0036584
0014793...

List Two:

0021545
0021585
0023625
0036584
0014793...

I am trying to create a macro that can tell me that number in red from list one is not in list two and that the number in red from list 2 is not in list one.

I am just having trouble visualizing the solution.

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

compare lists with fc (file compare)

Post by adroege » Mon May 17, 2010 5:07 pm

Is order significant in comparing the lists?

Are the lists in sorted order to begin with?


Try running the DOS command FC on the two text files
and examining the output


Is this similar to what you are looking for?

i.e.
C:\>fc list1.txt list2.txt

Erhull3287
Newbie
Posts: 10
Joined: Fri Apr 30, 2010 3:10 pm

Post by Erhull3287 » Mon May 17, 2010 5:10 pm

The numbers may not necessarily be in the same order in each list. I have gotten it to compare the lines one line at a time but it wouldnt work if the two files were out of order or if one list was missing a number etc. I can try the DOS command and see how that works out thanks!

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Mon May 17, 2010 5:17 pm

Try the following:

C:\>fc /LB1 list1.txt list2.txt


You may need to experiment with the parameters a bit to get
what you are after.


Issue the command C:\>FC /? to get a list of all the parms

Erhull3287
Newbie
Posts: 10
Joined: Fri Apr 30, 2010 3:10 pm

Post by Erhull3287 » Mon May 17, 2010 5:35 pm

It seems as if the DOS command goes through the lists 1 line at a time and compares line one to line 1 etc. It was also including other lines before and after the mismatched lines which was getting quite confusing. It looks like it is in the right direction but not exactly what I am looking for yet.

There is a way to do it in excel using conditional formatting which works fine but takes time copying the lists into an excel file etc. I am sure there is a way for Macro Scheduler to do it but I just cant pinpoint the solution!!

Erhull3287
Newbie
Posts: 10
Joined: Fri Apr 30, 2010 3:10 pm

Post by Erhull3287 » Mon May 17, 2010 5:39 pm

Sorry I will continue to try the DOS command changing the params., It looks like it is on the right track.

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

Post by adroege » Mon May 17, 2010 5:45 pm

Try reading in the lists to an array.

Then create a subroutine which loops through
every array item looking for a match with the
passed in parameter.

The main logic would be:

1. Read each line in list1 (at EOF quit)
2. execute the subroutine looking for that number in array2 (list2)
3. If it doesn't exist, make a note of it
4. Go back to step 1


Do the same logic again, but this time read list2 looking
in the array1 (list1) for mis-matches.

Erhull3287
Newbie
Posts: 10
Joined: Fri Apr 30, 2010 3:10 pm

Post by Erhull3287 » Mon May 17, 2010 6:03 pm

This sounds promising!

I can play around with this. I am still new to it all so Ill have to do some fiddling around to figure out creating the arrays but it sounds like that will work! Thanks!

Erhull3287
Newbie
Posts: 10
Joined: Fri Apr 30, 2010 3:10 pm

Post by Erhull3287 » Mon May 17, 2010 8:29 pm

ReadFile>C:\Users\Preferred User\Desktop\List1.txt,List1
Separate>List1,CR,Array1

ReadFile>C:\Users\Preferred User\Desktop\List2.txt,List2
Separate>List2,CR,Array2



Messagemodal>Array1_count


if>Array1_count=0,End
Let>k=0
Repeat>k
Label>Continue
Let>k=k+1
MessageModal>Array1_%k%

Gosub>Search

If>Array1_%k%=,End

Until>k=Array1_count

Label>End









SRT>Search

if>Array2_count=0,End
Let>x=0
Repeat>x

Let>x=x+1


If>Array2_%x%=Array1_%k%
Goto>Continue
Else
goto>searchmore
EndIF
Label>searchmore

Until>x,Array2_count
Let>A1=Array1_%k%
MessageModal>%A1% not found in list 2


END>Search











This is what I have come up with thus far. It is working just as I would like but for some reason it continues the loop forever instead of ending at Array1_count....I can't figure out why it won't stop but I am sure it is some simple mistake in the loops....

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

search arrays for items in list

Post by adroege » Mon May 17, 2010 10:43 pm

Try This. I think it is bad programming practice to use Goto to jump outside of a subroutine. Better to let the subroutine end and then test to see what happened after the code resumes after the Gosub.

I also think you needed to use a delimiter of CRLF instead of just CR.

[code]
ReadFile>C:\List1.txt,List1
Separate>List1,CRLF,Array1

ReadFile>C:\List2.txt,List2
Separate>List2,CRLF,Array2

Messagemodal>Array1_count

if>Array1_count=0,End
if>Array2_count=0,End

Let>k=0
Repeat>k
Let>k=k+1
MessageModal>Array1_%k%
Gosub>Search
If>Found=False
Let>A1=Array1_%k%
MessageModal>%A1% not found in list 2
Endif
Until>k=Array1_count

Label>End


SRT>Search
Let>Found=False
Let>x=0
Repeat>x
Let>x=x+1
If>Array2_%x%=Array1_%k%
Let>Found=True
Goto>Search_x
EndIF
Until>x,Array2_count
Label>Search_x
END>Search
[/code]

Erhull3287
Newbie
Posts: 10
Joined: Fri Apr 30, 2010 3:10 pm

Post by Erhull3287 » Tue May 18, 2010 12:19 pm

Hey that works perfect. I appreciate your taking your time to help! It works perfect and is much much cleaner!

Thanks again!

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts